Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何";手表;C+的大小+;gdb中的std::vector?_C++_Gdb_Stdvector - Fatal编程技术网

C++ 如何";手表;C+的大小+;gdb中的std::vector?

C++ 如何";手表;C+的大小+;gdb中的std::vector?,c++,gdb,stdvector,C++,Gdb,Stdvector,我有一个std::vector作为类的一部分,它包含一个自定义类型。它的内容似乎从节目的某个地方神秘地改变了。我很难弄清楚这是在哪里发生的 有没有一种方法可以从gdb“查看”std::vector的内容(或大小) 谢谢。我想那会有帮助的 有没有一种方法可以从gdb“查看”std::vector的内容(或大小) 假设您正在使用GCC,请在向量->\u M\u impl.\u M\u start和\u M\u finish上设置观察点。如果您使用的是其他一些std::vector实现,请进行相应的调

我有一个std::vector作为类的一部分,它包含一个自定义类型。它的内容似乎从节目的某个地方神秘地改变了。我很难弄清楚这是在哪里发生的

有没有一种方法可以从gdb“查看”std::vector的内容(或大小)

谢谢。

我想那会有帮助的

有没有一种方法可以从gdb“查看”std::vector的内容(或大小)

假设您正在使用GCC,请在向量->\u M\u impl.\u M\u start和
\u M\u finish
上设置观察点。如果您使用的是其他一些std::vector实现,请进行相应的调整

例如:

#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...
#包括
int main()
{
std::向量v;
v、 推回(1);
v、 推回(2);
}
g++-g t.cc
gdb-q./a.out
从/tmp/a.out读取符号…完成。
(gdb)启动
0x40090f处的临时断点1:文件t.cc,第5行。
临时断点1,t.cc处的main():5
5 std::载体v;
(gdb)n
7 v.推回(1);
(gdb)p v.M____________________启动
$1=(整数*)0x0
(gdb)p v.M___________完成
$2=(整数*)0x0
(gdb)p&v.安装完成
$3=(整数**)0x7fffffffd878
(gdb)手表*$3
硬件观察点2:$3
(gdb)p&v.\u M\u项目启动
$4=(整数**)0x7fffffffd870
(gdb)手表*$4
硬件观察点3:$4
(gdb)c
硬件观察点3:$4
旧值=(int*)0x0
新值=(int*)0x604010
std::vector::_M_insert_aux(this=0x7fffffffd870,_位置=0x0)位于/usr/include/c++/4.4/bits/vector。tcc:365
365此->\u M\u impl.\u M\u finish=\u new\u finish;
(gdb)英国电信
#0 std::vector::_M_insert_aux(this=0x7fffffffd870,_位置=0x0)位于/usr/include/c++/4.4/bits/vector。tcc:365
#在/usr/include/c++/4.4/bits/stl_vector.h:741处,std::vector::push_back(this=0x7fffffffffd870,_x=@0x7fffffffffd88c)中有1个0x0000000000400a98
#t.cc:7处的2 0x0000000000400935主管道()
(gdb)c
硬件观察点2:$3
旧值=(int*)0x0
新值=(int*)0x604014
std::vector::_M_insert_aux(this=0x7fffffffd870,__位置=0x0)位于/usr/include/c++/4.4/bits/vector。tcc:366
366此->存储的\u M\u impl.\u end\u=\u新\u start+\u len;
(gdb)英国电信
#0 std::vector::_M_insert_aux(this=0x7fffffffd870,_位置=0x0)位于/usr/include/c++/4.4/bits/vector。tcc:366
#在/usr/include/c++/4.4/bits/stl_vector.h:741处,std::vector::push_back(this=0x7fffffffffd870,_x=@0x7fffffffffd88c)中有1个0x0000000000400a98
#t.cc:7处的2 0x0000000000400935主管道()
... 等

您能否在大小增加的地方隔离和定义代码部分,并提供更多信息(例如,您的自定义类型是什么,自身是否分配内存等)?您仍然可以在向量的allocate()中放置一个断点,因为std::vector应该只有一个模板实现,所以源代码应该可以访问。我知道大小增加发生在哪里,但是当size函数稍后被另一个函数调用时,它会将其大小报告为0,我似乎不知道在哪里。向量包含指向不执行显式新建/删除操作的数据结构的指针。如果您向我们展示了一些有关该特定区域的代码,可能会有所帮助。否则(关于你对第一个贴出的答案的评论),我的巫毒叮当声只会让我保持沉默;o) 嗯。。。让我考虑一下如何显示有帮助的代码。我想不出一种不进入程序的类结构就可以显示代码的简单方法……我试过了,但从中我所能收集到的只是一种打印std::vector的方法。不知道怎么看…你能详细解释一下你的方法吗?因为我还是不懂你的方法?非常感谢。1美元,2美元。。。我们是gdb。它们可以用在表达式中。在本例中,您保留一个指向_M_finish变量的指针$3,该指针指向最后一个值。然后观察它所指向的值(*$3),因此,当_M_finish更改(添加/删除新元素)时,您将到达观察点。