正如您可以看到的一样,这个程序会导致段错误。调用 gdb 来看一下这个问题,如下所示:
清单 10. 调用 gdb
# gdb ./gdbtest1
GNU gdb 6.2.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "ppc-SUSE-linux"...Using host libthread_db
library "/lib/tls/libthread_db.so.1".
(gdb)
您知道段错误是在数字框 "9" 被删除之后发生的。执行 run 和 where 命令来精确定位段错误发生在程序中的什么位置。
清单 11. 执行 run 和 where 命令
(gdb) run
Starting program: /root/test/gdbtest1
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Program received signal SIGSEGV, Segmentation fault.
0x10000f74 in NumBox<int>::GetNext (this=0x0) at gdbtest1.cpp:14
14 NumBox<T>*GetNext() const { return Next; }
(gdb) where
#0 0x10000f74 in NumBox<int>::GetNext (this=0x0) at gdbtest1.cpp:14
#1 0x10000d10 in NumChain<int>::RemoveBox (this=0x10012008,
item_to_remove=@0xffffe200) at gdbtest1.cpp:63
#2 0x10000978 in main (argc=1, argv=0xffffe554) at gdbtest1.cpp:94
(gdb)