Solved

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

Posted on 2001-06-06
13
265 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

774 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