Using the Visual Studio .NET QuickWatch window

According to the MSDN documentation you can write C++ expressions in the QuickWatch window and have them evaluated. There are some documented limitations but in practice getting something to work in the QuickWatch window is very hit and miss. If you want to debug STL you run up against the limitations pretty quickly. Say we have the following code:
  vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);

If we run this code and break into the debugger you'd hope that you could inspect the values in the vector by typeing things like a[0] into the QuickWatch window, but alas this results in:
a[0]    CXX0058: Error: overloaded operator not found

Okay so lets try a.at(0):
a.at(0)    
CXX0039: Error: symbol is ambiguous

Hmmm, a.capacity() and a.size() work okay but how do we inspect the values? It seems the only reliable way is to rely on some knowledge of the underlying implementation:
 (a)._Myfirst[0]

or if you want to see all the values:
(a)._Myfirst,3

the ',3' is a hint to the expression evaluator telling it that its an array of size 3.

One thing that is useful is being to call custom print functions to output data to stdout on demand during debugging. So lets add a DumpVector function to our code:
void DumpVec(const vector<int>& a)
{
cout << "Dumping vector of size "<<(unsigned int)a.size()<<"\n";
for (unsigned int i=0;i<a.size();++i)
cout << setw(5) << i << setw(10)<<a[i]<<"\n";
}

Alas we are thwarted yet again when we type the following into the QuickWatch window:
DumpVec(a)

results in :
DumpVec(a)    CXX0047: Error: argument list does not match a function

I'm not quite sure why this happens but I'm guessing that the expression evaluator isn't able to convert our local stack instance into a const reference! Lets try rewriting the function to take a pointer as an argument instead:
void DumpVec(vector<int> const * a)
{
cout << "Dumping vector of size "<<(unsigned int)a->size()<<"\n";
for (unsigned int i=0;i<a->size();++i)
cout << setw(5) << i << setw(10)<<(*a)[i]<<"\n";
}

Now when we type:
 DumpVec(&a)

We get:
    DumpVec(&a)    <void>    void

in the value window and the console contains:
Dumping vector of size 2
0 1
1 2

At last!

Comments

Popular posts from this blog

Using Python to Automate Visio

Microsoft accounts for children - Verify that you're an adult error

The computer says no