我实现了一个递归算法,该算法运行了大约 1730 次递归,然后因神秘的 SIGSEGV 而崩溃。我尝试了我的 gdb 并得到以下输出:
Program received signal SIGSEGV, Segmentation fault.
0x000000000040b7da in Town::get_cur_capacity (this=0x61efe0) at ./solver/Darstellung.cpp:54
54 return left_over_capacity;
(gdb) print 0x61efe0
$1 = 6418400
(gdb) print *0x61efe0
Cannot access memory at address 0x61efe0
(gdb) print this
$2 = (const Town * const) 0x61efe0
(gdb)
Run Code Online (Sandbox Code Playgroud)
调试器怎么知道它应该是一个 const Town 指针,但无法访问内存给我转储?我很确定这个方法中没有错误,因为它在崩溃前被使用了几千次,就像程序中的所有其他函数一样。有没有可能这是与操作系统相关的问题?我正在使用 Linux Ubuntu 64 位。
我的简化算法:
bool solveproblem(ptr_to_model) {
if( way_one(ptr_to_model) )
return true;
if(way_two(ptr_to_model) )
return true;
if( check_if_solved)
return true;
return false;
}
bool way_one(ptr_to_model) {
for(go_through_current_problem_configuration) …Run Code Online (Sandbox Code Playgroud) 我的问题与此有关,因为我解决了问题,我编写了自己的排序算法(简单的插入排序),并且它有效.我对此感到非常惊讶,因为我认为标准库已经过充分测试.是否有任何已知的特殊情况std::sort可能会搞乱?