Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何了解c程序';s堆栈溢出?_C_Debugging_Stack Overflow - Fatal编程技术网

如何了解c程序';s堆栈溢出?

如何了解c程序';s堆栈溢出?,c,debugging,stack-overflow,C,Debugging,Stack Overflow,我正在用c模拟一个问题(3d Ising模型),但当问题规模变大时,程序停止,出现以下错误: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) 在这个程序中,我有一个递归函数来完成所有的工作,我怀疑错误是由于堆栈溢出(在递归函数中),但我不知道如何确定 如果是因为堆栈溢出,有没有办法在不改变程序设计的情况下解决这个问题 我正在使用Clion IDE /* * recursive function t

我正在用c模拟一个问题(3d Ising模型),但当问题规模变大时,程序停止,出现以下错误:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
在这个程序中,我有一个递归函数来完成所有的工作,我怀疑错误是由于堆栈溢出(在递归函数中),但我不知道如何确定

如果是因为堆栈溢出,有没有办法在不改变程序设计的情况下解决这个问题

我正在使用Clion IDE

/*
 * recursive function to form Wolff Cluster(= WC)
 */
void grow_Wolff_cluster(lattic* l, Wolff* wolff, site *seed){

    /*a neighbor of site seed*/
    site* neighbor;

    /*go through all neighbors of seed*/
    for (int i = 0 ; i < neighbors ; ++i) {


        neighbor = seed->neighbors[i];

        /*add to WC according to the Wolff Algorithm*/
        if(neighbor->spin == seed->spin && neighbor->WC == -1 && ((double)rand() / RAND_MAX) < add_probability)
        {
            wolff->Wolff_cluster[wolff->WC_pos] = neighbor;
            wolff->WC_pos++;                  // the number of sites that is added to WC
            neighbor->WC = 1;          // for avoiding of multiple addition of site
            neighbor->X = 0;


            ///controller_site_added_to_WC();


            /*continue growing Wolff cluster(recursion)*/
            grow_Wolff_cluster(l, wolff, neighbor);
        }
    }
}
/*
*形成Wolff群集(=WC)的递归函数
*/
void grow_Wolff_簇(晶格*l,Wolff*Wolff,站点*seed){
/*种子的邻居*/
站点*邻居;
/*通过种子的所有邻居*/
对于(int i=0;i<邻居;++i){
邻居=种子->邻居[i];
/*根据Wolff算法添加到WC*/
如果(相邻->旋转==种子->旋转和相邻->WC==-1&&((双)rand()/rand\u MAX)wolff_集群[wolff->WC_位置]=邻居;
wolff->WC_pos++;//添加到WC的站点数
邻居->WC=1;//避免多次添加站点
邻居->X=0;
///控制器_站点_已将_添加到_WC();
/*继续增长Wolff群集(递归)*/
增长沃尔夫集群(l,沃尔夫,邻居);
}
}
}

使用GDB调试器并查看调用堆栈

gdb main
r
bt

在调试器中捕获崩溃。查看函数调用堆栈。如果它只包含
grow\u Wolff\u cluster
调用,那么就会出现堆栈溢出。递归算法通常会产生优雅的解决方案,但实际上由于这些原因,它们可能是危险的,通常应该避免,除非您确定它只会安全地递归多次。我不知道您在这里做什么,但最好是以非递归方式实现它。应该是有帮助的阅读。也可能是为了回到可靠工作的坚实基础上,然后再次开始递增。最基本的方法:在程序的关键点放置一些printf,然后查看显示的内容。是的,它充满了递归函数,速度太慢,我无法导航以查看其中有多少。但毕竟我有一个递归函数,我希望看到很多递归函数。谢谢,我会试试这个,GDB是默认安装的?!