Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging C++ code involving use of: vector, string, STL

Tags:

c++

debugging

I am C++ beginner. When I try to debug C++ code using following constructs like string, vector of certain native types, STL etc, debugging gets tedious. I use MS Visual Studio 2010/ Visual C++ 2010 Express.

e.g.

-- While using string as below:

string str;

getline(cin, str);

for(i=0; i<str.size();i++)

Watch window does not show values for str[i]. It says overloaded operator not found. I have to manually collapse the whole string variable str and see the char elem at that particular index, which gets cumbersome.

-- While using vector as below, same issue. If I set variable v1[k] in watch window same error.

vector<int> v1(100);


for(int k=0;k<100;k++)
{
    v1.push_back(k);
}

-- Tried using simple STL iterators like it.begin() , it.end() and algorithms like sort(), reverse() , I could not debug inside those functions by stepping, or could not set break point into those.(I know they being inside STL or some such standard library they would be assured to be bug-free, but one can still use them incorrectly by passing something invalid/incorrect)

Coming from C language usage of many years, to C++, I find this lack of 'debug ability' , or some restrictions in that , painful, while I am trying to understand large chunks of C++ code written by someone else, at work.

My questions -

What are effective ways to debug working code to understand its functionality while using idioms like step in, breakpoints, watch point, watch windows. Is any particular debugger better/worse than other.(Like say gdb being better) or are there any specific tricks/tips to aid debugging.

In general how to analyze a C++ code to understand its working?

like image 493
goldenmean Avatar asked Sep 24 '11 17:09

goldenmean


2 Answers

As you have found out, trying to use overloaded operators in the watch window simply won't work. You need to break open the objects and pull out the member variables.

In MSVC, std::vector has a member variable _Myfirst that points to the first element of its buffer. To get the item at index i you can add

(v._Myfirst)[i]

To the watch window. You can also use

(v._Myfirst),10

To show the first 10 elements of the array.

There should be a similar member variable for std::string.

like image 126
Peter Alexander Avatar answered Oct 11 '22 08:10

Peter Alexander


A very small amount of types have "debug visualizers" specified for them to assist in debugging. They are in general a fantastic help and I find it almost impossible to get anything done without them now (why do I care about the implementation of a vector.. I just want to know what's in it!)

If, however, you do want to disable them, google around for the "autoexp.dat" file that controls that. You can just remove a few lines in that, and everything will go back to flat types. I will warn you that things like maps and sets become essentially un-debuggable without the visualisers.

Alternatively, switch into C++/CLI. VS2010 doesn't support debug visualisers in C++/CLI, which is usually a tremendous frustration, but I guess may be what you're looking for.

like image 37
Ayjay Avatar answered Oct 11 '22 09:10

Ayjay