Solved

Can you dynamically allocate memory and see it in debug mode?

Posted on 2001-06-06
13
263 Views
Last Modified: 2010-05-19
I am using VC++ 6.0 Developers Studio running under Windows NT 4.0.  How can I dynamically allocate memory for a sturcture and still be able to see it in the watch window in debug. I can create the structure using NEW but it only gives me a memory address because it's a pointer.  I'd like to be able to allocate and still see the variables in the watch window.  Is this even possible?

Thanks.
0
Comment
Question by:geraldb
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
If you click on the + sign by the variable name (on the left in the debug window), it will expand to show you the structure members.  At least it does under Windows 2000, and NT shouldn't be any different.
0
 
LVL 4

Accepted Solution

by:
prashant_n_mhatre earned 200 total points
Comment Utility
I don't have access to VC++ now, but tomorrow I can tell you for sure.

But see how it works.

Normally when you allocate an array using new or new/malloc/calloc in VC++ when you look at the variable in the watch window you can only see the first element. To view the entire array just type the <variable-name>, <number-of-elements-to-view>. Example:
 
void main( void )
{

          const int SIZE = 10;
          int *intArray = 0;
          intArray = (int *)malloc( sizeof( int ) * SIZE ) ;

          for( int x = 0; x < SIZE; x++ )

          {
                    intArray[x] = 10 + x;  
          }

}

to see intArray in the watch window type

intArray, 10

and it will expand out for the entire arrayTo see first 5 elements Say
intArray, 5
To see next 4 elements from intArray[3] say
(intArray + 3), 5


Extend the same philosophy for structure. It should work.
0
 

Expert Comment

by:robertmitchell_99
Comment Utility
The alternatives for viewing variables are:

1. Do as the previous answerer suggested and expand the    
variables window's + sign.

2. open the quick watch (rt click context menu) and enter the pointer name.

3. copy the address (0x???) from the watch window, open the memory window (Alt + f6) place the pointer address in the top text box and you can see the bits which make up the structure.

4. use the -> operator in the main watch window to view a member's value e.g. if the pointer you have allocated to with new is p and your struct has a member m enter p->m in the main watch window to view it's value

5. To assign to variables while you're debugging use the quick watch.
Place the pointer's name in the top expression box, click on the value you wish to change, enter the value you wish to assign and click the recalculate button (you can use expressions as well as values).

You can then see the changed value in the variables window, watch window, memory window etc.

If you want to check out visual studio's debug watch support go to view->Debug windows in the main menu and have a browse, youll see it's pretty good.

Best of luck,

Robert
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
See the following link for the correct answer:
http://codeguru.earthweb.com/tips/CustomizingDebug.shtml
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
The link I posted above shows you how to modify the autoexp.dat file so that you can automatically see the value of your variable in the debug window.

This is the method that CString uses, and that's why you can see the value of CString with out clicking the plus sign.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
geraldb,
Did you forget about your question.
Please give us some feedback.

Thank you
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:geraldb
Comment Utility
This was a very simple but effective way to see the values I wanted to see.  I'd like to know how to look at a two dimensional array in the same way.
Thanks.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Thank you for the points&grade.

Can you give me an example of the variable?
Like the variable declaration.
0
 

Author Comment

by:geraldb
Comment Utility
To: Axter.
I tried the method you suggested but couldn't quite get it to work for my data types.  I think it would be a better method than having to type in a , x for the number of elements to view.  Could you give me more info on how to setup the datatype in autoexp.dat.   This is an example of the data I am trying to view.

     int **CornerPts;
        int iMaxNumSections = 50;
        int iHeadsPerRing = 4;

     CornerPts = new int*[iMaxNumSections];

     for(i=0;i<iMaxNumSections;i++)
          CornerPts[i] = new int[iHeadsPerRing];

It gives a two dimensional array of ints.
Thanks for the help.
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
geraldb

To view 2-D matrix, say
A[2][3]

You can view the elements the row by the same way.

A[0],3 will give you the all elements in the first row
A[1],3 in second row and so on...

i.e., A[row_num], no_of_elements
Same philosphy you can extend for offset...

0
 

Author Comment

by:geraldb
Comment Utility
To: Axter.
I tried the method you suggested but couldn't quite get it to work for my data types.  I think it would be a better method than having to type in a , x for the number of elements to view.  Could you give me more info on how to setup the datatype in autoexp.dat.   This is an example of the data I am trying to view.

     int **CornerPts;
        int iMaxNumSections = 50;
        int iHeadsPerRing = 4;

     CornerPts = new int*[iMaxNumSections];

     for(i=0;i<iMaxNumSections;i++)
          CornerPts[i] = new int[iHeadsPerRing];

It gives a two dimensional array of ints.
Thanks for the help.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
geraldb,
Oops!  I miss read the accept answer.
I'm sorry, but I don't think my solution would work for your variable type.
0
 

Author Comment

by:geraldb
Comment Utility
To: Axter.
I tried the method you suggested but couldn't quite get it to work for my data types.  I think it would be a better method than having to type in a , x for the number of elements to view.  Could you give me more info on how to setup the datatype in autoexp.dat.   This is an example of the data I am trying to view.

     int **CornerPts;
        int iMaxNumSections = 50;
        int iHeadsPerRing = 4;

     CornerPts = new int*[iMaxNumSections];

     for(i=0;i<iMaxNumSections;i++)
          CornerPts[i] = new int[iHeadsPerRing];

It gives a two dimensional array of ints.
Thanks for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now