Microsoft KB Archive/328578

= How to view the address of variables in Managed Extensions for C++ Debugger =

Article ID: 328578

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q328578



SUMMARY
Because of a bug in the Visual C++ .NET and Visual C++ 2005 Managed Extensions for C++ Debugger, you cannot obtain the memory address of a variable. This article describes how to use the Registers window and Disassembly window of the source to work around the bug, and how to obtain the memory address. When you have the address of the variable that you want to view, you can view its contents in the Memory window of the debugger.



MORE INFORMATION
To obtain the memory address, you must monitor in the Disassembly window when your variables and pointers are initialized. The Disassembly window displays the basic instructions that the compiler generates for your source code. Most of the time, the address of the memory that is being modified is stored in a register or an offset from the contents of a register. The Disassembly window shows you the register that receives the address of the variable. Basic knowledge of assembly language might help in this scenario.

When you have obtained this data, follow these steps:
 * 1) To open the Registers window, on the Debug menu, point to Windows, and then click Registers.
 * 2) To get the value of the register from the Registers window, form the arithmetic expression that resolves to the address, and then type it in the Memory window.

Consider the following simple program: // testmem.cpp // compile it with the /clr and /Zi switches.


 * 1) using 
 * 2) include 

int _tmain(int argc, _TCHAR* argv[]) {   int x = 5;

int __nogc* pX = &x;

int __gc* y = new int( 100 );

return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article.

The following is a disassembled view that the debugger shows for the program that is listed earlier. The comments have been added to show which register reveals the address of the variables.

--- testmem.cpp -
 * 1) using 
 * 2) include 

int _tmain(int argc, _TCHAR* argv[]) {   int x = 5; 00000000 push        ebp 00000001 mov         ebp,esp 00000003 sub         esp,18h 00000006 push        edi 00000007 push        esi 00000008 push        ebx 00000009 mov         dword ptr [ebp-4],ecx 0000000c mov         dword ptr [ebp-8],edx 0000000f xor         edi,edi 00000011 xor         ebx,ebx 00000013 mov         dword ptr [ebp-14h],0 0000001a mov         dword ptr [ebp-18h],5         // epb-0x18 holds &x int __nogc* pX = &x; 00000021 lea         eax,[ebp-18h] 00000024 mov         dword ptr [ebp-14h],eax           // eax holds pX    int __gc* y = new int( 100 ); 00000027 mov         ecx,4 0000002c call        dword ptr ds:[00935104h] 00000032 mov         esi,eax               // eax holds y

00000034 mov         edi,esi 00000036 test        edi,edi 00000038 je          00000044 0000003a mov         dword ptr [edi],64h           // edi holds y 00000040  mov         esi,edi 00000042 jmp         00000046 00000044 xor         esi,esi 00000046 mov         ebx,esi return 0; 00000048 nop 00000049 jmp         0000004B } 0000004b xor         eax,eax 0000004d pop         ebx 0000004e pop         esi } 0000004f pop         edi 00000050 mov         esp,ebp 00000052 pop         ebp 00000053 ret

