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

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.
geraldbAsked:
Who is Participating?
 
prashant_n_mhatreConnect With a Mentor Commented:
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
 
mnashadkaCommented:
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
 
robertmitchell_99Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AxterCommented:
See the following link for the correct answer:
http://codeguru.earthweb.com/tips/CustomizingDebug.shtml
0
 
AxterCommented:
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
 
AxterCommented:
geraldb,
Did you forget about your question.
Please give us some feedback.

Thank you
0
 
geraldbAuthor Commented:
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
 
AxterCommented:
Thank you for the points&grade.

Can you give me an example of the variable?
Like the variable declaration.
0
 
geraldbAuthor Commented:
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
 
prashant_n_mhatreCommented:
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
 
geraldbAuthor Commented:
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
 
AxterCommented:
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
 
geraldbAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.