3

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.

Narek
  • 38,779
  • 79
  • 233
  • 389

3 Answers3

11

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.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • why not? how would you define convenient? – Marius Bancila Jan 31 '13 at 08:34
  • 1
    The problem is that every time you should go to the vector file, step out several times and come to the place where you want to see the vector's content. The second problem is that each reallocation will cause a break. – Narek Jan 31 '13 at 08:40
  • 2
    you want to get notified that the size changes. of course reallocations trigger breaks – Marius Bancila Jan 31 '13 at 13:15
  • 1
    Oh boy! This works for conditional breakpoints! Suppose you want to break when a vector (in a local) is not empty, then the condition is `myvec._Mylast != myvec._Myfirst` !! Thanks – davidbak Jan 17 '18 at 22:57
0

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).

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • I haven't used watchpoints. I just checked it out but it seems like they don't work like breakpoints (the debugger doesn't stop when the variable is modified). I think you'll have to use breakpoints like I described. – user1610015 Jan 31 '13 at 07:56
  • 1
    Also this will break for each and every vector. But I may have thousands of vectors. And I need to debug only one of them. – Narek Feb 04 '13 at 09:23
0

Complementing the @Marius Bancila answer above, in my case the std::vector implementation is more intricate, and its size implementation is:

enter image description here

_NODISCARD size_type size() const noexcept {
    auto& _My_data = _Mypair._Myval2;
    return static_cast<size_type>(_My_data._Mylast - _My_data._Myfirst);
}

I suspect it has been changed in a recent version of Visual Studio, as I am using the Visual C++ 2019 compilation toolkit.

So to watch:

first:

yourVector._Mypair._Myval2._Myfirst

last:

yourVector._Mypair._Myval2._Mylast
sergiol
  • 4,122
  • 4
  • 47
  • 81