Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "watch" the size of a C++ std::vector in gdb?

Tags:

I have a std::vector as part of a class, that contains a custom type. Its contents seems to be mysteriously changed from somewhere in the program. I am having trouble trying to figure out where this is happening.

Is there a way to "watch" the contents (or size) of a std::vector from gdb?

Thanks.

like image 508
endbegin Avatar asked Nov 23 '11 21:11

endbegin


People also ask

How do you check the size of a vector in a vector?

To get the size of a C++ Vector, you can use size() function on the vector. size() function returns the number of elements in the vector.

How do you define the size of a vector in C++?

The C++ function std::vector::resize() changes the size of vector. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of vector. If val is specified then new elements are initialed with val.

How do you use sizeof in vector?

Use the vector::size() method: i < v. size() . The sizeof operator returns the size in bytes of the object or expression at compile time, which is constant for a std::vector .

Does vector have size?

Important concepts to learn: The vector size and its capacity and what the difference between them is. The capacity is the number of elements the vector have allocated memory for. The size is the current number of elements in the vector.


1 Answers

Is there a way to "watch" the contents (or size) of a std::vector from gdb?

Assuming you are using GCC, set watchpoints on theVector->_M_impl._M_start and _M_finish. If you are using some other std::vector implementation, adjust accordingly.

Example:

#include <vector>  int main() {   std::vector<int> v;    v.push_back(1);   v.push_back(2); }  g++ -g t.cc gdb -q ./a.out  Reading symbols from /tmp/a.out...done. (gdb) start Temporary breakpoint 1 at 0x40090f: file t.cc, line 5.  Temporary breakpoint 1, main () at t.cc:5 5     std::vector<int> v; (gdb) n 7     v.push_back(1); (gdb) p v._M_impl._M_start $1 = (int *) 0x0 (gdb) p v._M_impl._M_finish  $2 = (int *) 0x0 (gdb) p &v._M_impl._M_finish $3 = (int **) 0x7fffffffd878 (gdb) watch *$3 Hardware watchpoint 2: *$3 (gdb) p &v._M_impl._M_start $4 = (int **) 0x7fffffffd870 (gdb) watch *$4 Hardware watchpoint 3: *$4 (gdb) c Hardware watchpoint 3: *$4  Old value = (int *) 0x0 New value = (int *) 0x604010 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365 365       this->_M_impl._M_finish = __new_finish; (gdb) bt #0  std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365 #1  0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, __x=@0x7fffffffd88c) at /usr/include/c++/4.4/bits/stl_vector.h:741 #2  0x0000000000400935 in main () at t.cc:7 (gdb) c Hardware watchpoint 2: *$3  Old value = (int *) 0x0 New value = (int *) 0x604014 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366 366       this->_M_impl._M_end_of_storage = __new_start + __len; (gdb) bt #0  std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366 #1  0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, __x=@0x7fffffffd88c) at /usr/include/c++/4.4/bits/stl_vector.h:741 #2  0x0000000000400935 in main () at t.cc:7  ... etc... 
like image 132
Employed Russian Avatar answered Nov 02 '22 08:11

Employed Russian