我有一个代码:
#include <list>
int f(std::list<int>& l)
{
l.clear();
int i = 0;
return i;
}
int main(int argc, char* argv[])
{
std::list<int> l;
int i = f(l);
i++;
}
我用三种方法构建它:
- g++ -g -o main1./main1.cpp
- g++ -g -o main1 - 01 ./main1.cpp
- g++ -g -o main1 -O2 ./main1.cpp
我用gdb (GNU gdb (gdb) 7.5.1)调试它,当我进入int f(std::list&l)我得到这样的输出:
Python Exception <type 'exceptions.IndexError'> list index out of range:
(gdb) Python Exception <type 'exceptions.IndexError'> list index out of range:
下面是我的gdb会话:
(gdb) bre main
Breakpoint 1 at 0x400603: file ../main1.cpp, line 11.
(gdb) r
Starting program: /home/mhd/Texts/Programming/Programms/Exercises/Linux/BruceMolayUnixLinux/Exercises/2/Head/Debug/main1
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
(gdb) n
(gdb) n
(gdb) s
Python Exception <type 'exceptions.IndexError'> list index out of range:
(gdb) Python Exception <type 'exceptions.IndexError'> list index out of range:
q
Debugger finished
如何防止此异常?为什么要抛出异常?
您可以通过禁用有问题的漂亮打印机来防止此异常。