这里我想解决此代码中的堆栈溢出问题。 这里在这段代码中,我调用函数p递归350000次,所以我得到了分段错误 当我删除350000并把300000比它工作正常 这里分段错误来,因为我调用递归调用函数p多次或调用递归函数太深。如何在C程序中删除此分段错误
这不工作,因为我采取if(i != 350000)
。 其停在可以是在该范围内300000到327480.我测试10次
CODE:
#include <stdio.h>
void p(char *, int);
int main()
{
char *a = "HI";
int b = 10;
p(a, b);
printf("\nComplete");
return 0;
}
void p(char *a, int b)
{
static long int i = 0;
if (i != 350000)
{
printf("\n%ld \t at Hi hello", i);
i++;
p(a, b);
} else
{
return;
}
}
当我使用的valgrind工具比valgraind报告错误这样
==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff8
==9236==
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236== Access not within mapped region at address 0x7FE801FF8
==9236== at 0x4EA012E: [email protected]@GLIBC_2.2.5 (fileops.c:1276)
==9236== If you believe this happened as a result of a stack
==9236== overflow in your program's main thread (unlikely but
==9236== possible), you can try to increase the size of the
==9236== main thread stack using the --main-stacksize= flag.
==9236== The main thread stack size used in this run was 8388608.
==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff0
==9236==
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236== Access not within mapped region at address 0x7FE801FF0
==9236== at 0x4A2269F: _vgnU_freeres (vg_preloaded.c:58)
==9236== If you believe this happened as a result of a stack
==9236== overflow in your program's main thread (unlikely but
==9236== possible), you can try to increase the size of the
==9236== main thread stack using the --main-stacksize= flag.
==9236== The main thread stack size used in this run was 8388608.
检查此代码
请帮助我,我真的想要这个问题的解决方案。我不能从我的代码递归调用函数的调用。
那么我如何知道当前的堆栈大小并以编程方式重置堆栈大小 –
您不需要在程序中执行它,它通常由链接器或加载器完成。严重的是,你需要重新审视你的递归需求。 – paxdiablo
回复我......我该怎么做? –