How to read the debug memory window in Visual Studio

29,018

Solution 1

One way to find the address of a variable in Visual Studio is to use the QuickWatch window (under the debug menu if you don't know the hot key, Ctrl + Alt + Q). If you type &a, it will display the address of variable a. You can then enter that address in the memory window. Or you could just enter &a in the memory window.

But to see all variables with the memory window, they would need to be within a few bytes of each other since it shows contiguous memory. For local variables on the stack, that would not usually be a problem. For integer variables, you can more easily view them in a readable format by right clicking on the memory window and changing the layout (for example, choose 4-byte integers with a signed display).

Having said all that, it seems like it would be much simpler to use the watch window since everything is labeled nicely already and it is easy to tell which value is associated with which variable.

Solution 2

I saw tons of "gibberish": a small example may help (especially for the next readers :)

  1. Copy/paste the following code and debug it:

    struct MyStruct
    {
        int age;
        char code_1;
        char code_2;
        char code_3;
    };
    
    int main()
    {
        int int_variable = 65;
    
        int* adresse_int_variable = &int_variable;
    
        int int_variable2 = 10000;
    
        char char_variable_1 = 'A';
    
        char char_variable_2 = 'B';
    
        cout << " sizeof(int_variable) " << sizeof(int_variable) << endl;
        cout << " sizeof(char_variable_1) " << sizeof(char_variable_1) << endl;
    
        MyStruct mystruct;
        mystruct.age = int_variable2;
        mystruct.code_1 = 'A';
        mystruct.code_2 = char_variable_2;
        mystruct.code_3 = int_variable;
        return 0;
    }
    
  2. Run the Visual Studio debugger, add watch to all variables (right click each variable and click "Add Watch"). Now if it is not open, open the watch window (menu DebugWindow → *Watch), and drag/drop the variable adresse_int_variable from the watch window to the memory window. You'll obtain the following:

    Enter image description here

  3. You'll observe that the value 41 appears at this address. In hexadecimal, 0x41 is equal to 65. So you see that the address of variable int_variable effectively contains 65. (Note that in reality the memory contains bits: 01000001, but it is represented in hexadecimal to ease of the reading.)

  4. Enter &int_variable2 in the memory window, you'll get:

    Enter image description here

    int_variable2 holds the value 10000, and in hexadecimal it is 0x2710. Now look for values stored for variables char_variable_1 and char_variable_2: you see 0x41 and 0x42. This is the way A and B are encoded in the ASCII Table. Note that in memory int_variable and char_variable_1 are the same.

  5. Finally enter &mystruct in the memory window, and you'll see:

    Enter image description here

    This corresponds to the memory of the mystruct variable, that holds four variables (an int and three chars). You see the age variable (10000 = 0x2710) and the three following characters: A , B and 65 that are stored as 0x41, 0x42, 0x41 (from right to left). Note that in the right part of the window you can see ABA as string representation of the memory (if not right click the window and click ANSI).

  6. Try with more complex variables, read about endianness and data structure alignment. See also the memory window page on MSDN.

Share:
29,018

Related videos on Youtube

Ivan
Author by

Ivan

Updated on November 12, 2020

Comments

  • Ivan
    Ivan over 3 years

    I have used debug mode in Visual Studio before, but I never had to use the memory window. If I had a simple application that calculates a=b+c and made b =8 and c=-2, how can I find the addresses a, b, and c in memory window and their values without using watches?

    When I tried, I saw tons of "gibberish" that I cannot make much sense of. Here's a screenshot:

    Enter image description here

    If I wanted to do the same, but in a Linux environment, how could I achieve this?

  • Peter Mortensen
    Peter Mortensen over 6 years
    This does seem to work for a .NET application (e.g. to see what some Unicode string really is) - "expression expected".

Related