C++ debugging by Visual Studio - Watchpoint on vec

2019-04-12 04:16发布

问题:

I want to explore the changes with my vector. Thus I want to set a whatchpoint on the vector size. Thereby, Visual Studio will let me to see what is in my vector after each size change. How I can do this?

Here in this link you can find how to set a conditional breakpoint. And I tried to set a condition like this: my_vect.size() variable on Has changed event (according to 8. Conditional breakpoints), but it sucks.

回答1:

my_vect.size() is not a variable, but a function. It looks like this:

size_type size() const _NOEXCEPT
    {   // return length of sequence
    return (this->_Mylast - this->_Myfirst);
    }

So here is the solution: start your program with the debugger. Break before the vector size changes. Add a New Data Breakpoint. Suppose your vector is called myvec. Then in the address field put &myvec._Mylast and respectively &myvec._Mylast. Now, the debugger will stop whenever the pointers to the first or last elements in the vector change.



回答2:

You can open the <vector> header and set a breakpoint at the start (or at the end) of each method of std::vector that changes the size of the vector (like push_back, erase, etc).