Search code examples
c++nativevisual-studio-debugging

Visual Studio Debugger Watch problems


How can I find out the address of a variable on the stack in the visual studio debugger watch window (or elsewhere?)

Reading the variable works fine:

streets     streets [11790](0x1c66a690 [...] std::vector<Street *,std::allocator<Street *> >

But prefixing with & doesn't give me an address:

&streets        streets [11790](0x1c66a690 [...] std::vector<Street *,std::allocator<Street *> >

Also, trying to read the size doesn't work, why is that?

streets.size()  CXX0075: Error: Cannot set up Function Evaluation   

The program is compiled in debug mode.


Solution

  • The Visual Studio Debugger drives debugger watch, quick-watch, auto, and local variable views through a translation defined by schema in a file called autoexp.dat (depending on your VS version, the content therein can vary markedly). The file is located in your VS-InstallDir/Common7/Packages/Debugger folder (at least it is for VS2010 and VS2012).

    Knowing this, a couple of ideas for you to try/consider:

    Method One: Library Know-How

    To access the actual address of the first element within the vector I ultimately just do this:

    streets._Myfirst
    

    if you know the number of elements you're going to view, you can use the array-expansion extension by:

    streets._Myfirst,N
    

    where N is the number of elements

    Note: this only works as shown above with vectors. The practice is different depending on which container you are using. There are no-doubt simpler ways that are probably less dependent on the implementation of std::vector<>, but this is the simplest wasy I know how to get you up and debugging quickly.


    Method Two: Scorched Earth

    Under Tools/Options/Debugging/General is a list of features you can switch on and off. One of them you will find particularly useful to this question:

    Show raw structure of objects in variable windows.

    Turn this ON to see raw member variables of all structures and containers, including standard containers like std::vector<>. This effectively disables usage of the templates in autoexp.dat