本文介绍了如何“观看”在gdb中的C ++ std :: vector的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个std :: vector作为类的一部分,包含一个自定义类型。它的内容似乎是从程序的某处神秘地改变。我遇到麻烦,想知道这是发生在哪里。
I have a std::vector as part of a class, that contains a custom type. It's contents seems to be mysteriously changed from somewhere in the program. I am having trouble trying to figure out where this is happening.
有一种方法来监视std :: vector的内容(或大小)从gdb ?
Is there a way to "watch" the contents (or size) of a std::vector from gdb?
感谢。
推荐答案
GCC,在 theVector-> _M_impl._M_start
和 _M_finish
上设置观察点。如果您正在使用其他std :: vector实现,请进行相应调整。
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.
示例:
#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...
这篇关于如何“观看”在gdb中的C ++ std :: vector的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!