c++ - Python 异常 : index out of range while debuging the code c++ with support for STL with gdb

标签 c++ python exception gdb g++

我有一个代码:

#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 种方式构建它:

  1. g++ -g -o main1 ../main1.cpp
  2. g++ -g -o main1 -O1 ../main1.cpp
  3. 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 session :

(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

如何防止这种异常?为什么抛出异常?

最佳答案

您可以通过禁用有问题的 pretty-print 来防止此异常。请参阅“帮助禁用 pretty-print ”。

关于c++ - Python 异常 : index out of range while debuging the code c++ with support for STL with gdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15689365/

相关文章:

c++ - 在异常构造函数中参数化错误消息是一种好习惯吗?

c# - 如何区分 InvalidOperationException 异常类型?

C++ 参数传递查询(包括代码示例和输出)

c++ - 返回函数参数 unique_ptr

c++ - 了解递归函数中的数组和指针

python - 为什么 python list 将每个字符存储在 BeautifulSoup 标签中?

c++ - MFC中ID模式问题

python - Cython:优化原生 Python 内存 View

python - 在不加载到内存的情况下将 HDF5 转换为 Parquet

java - 无法将 EAR 发布到 Websphere