使用STL列表调试问题

gdb and debugging problems with an STL list

本文关键字:问题 调试 列表 STL 使用      更新时间:2023-10-16

我最近在这里问了一个问题,不幸的是没有得到一个我可以使用的答案:

c++ STL列表函数用空列表分段

我一直在尝试使用gdb来调试这个问题,我有一个关于什么意思的问题:

我将list声明为类的成员,如下所示:
std::list<Thing*> inventory;

…然后实例化它所在的类(一个名为"pc"的对象)。在gdb中,我假定这表明我为它分配了一些内存。

(gdb) p &pc.inventory
$7 = (std::list<Thing*, std::allocator<Thing*> > *) 0xbffff22c

进一步,在代码的问题行前一行(实际上,调用'inventory.size()'会导致段错误),这仍然成立:

(gdb) p &inventory
$8 = (std::list<Thing*, std::allocator<Thing*> > *) 0xbffff22c

…但是我仍然得到segfault:

(gdb) n
558   if (inventory.size() == 52) {
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0804e3fe in std::_List_const_iterator<Thing*>::operator++ (this=0xbfff94e0)
at /usr/include/c++/4.4/bits/stl_list.h:223
223     _M_node = _M_node->_M_next;

我的问题本质上是这样的:当然,如果我有一个成员列表的地址,列表存在,我应该能够在它上面使用size() ?如果不是,为什么不呢?我该如何进一步调试?

谢谢!

要调试内存错误,我建议您在valgrind

中运行程序。
valgrind——tool=memcheck program_name

和修复valgrind

指向的所有错误