我有一个代码:
#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++;
}
我以 3 种方式构建它:
- g++ -g -o main1 ../main1.cpp
- g++ -g -o main1 -O1 ../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
如何防止此异常?为什么会抛出异常?