Solved

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

Posted on 2001-06-06
13
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 6161647
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
ID: 6161900
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
ID: 6161970
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

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

Expert Comment

by:Axter
ID: 6162161
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
ID: 6182853
geraldb,
Did you forget about your question.
Please give us some feedback.

Thank you
0
 

Author Comment

by:geraldb
ID: 6182907
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
ID: 6182930
Thank you for the points&grade.

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

Author Comment

by:geraldb
ID: 6182948
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
ID: 6183023
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
ID: 6183031
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
ID: 6183059
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
ID: 6183069
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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