简体   繁体   English

限制C ++中的递归调用(约5000)?

[英]Limit recursive calls in C++ (about 5000)?

In order to know the limit of the recursive calls in C++ i tried this function ! 为了知道C ++中递归调用的限制,我尝试了这个函数!

void recurse ( int count ) // Each call gets its own count
{
printf("%d\n",count );
  // It is not necessary to increment count since each function's
  //  variables are separate (so each count will be initialized one greater)
  recurse ( count + 1 );
}

this program halt when count is equal 4716 ! 当计数等于4716时,该程序停止! so the limit is just 4716 !! 所以限制只有4716 !! I'm a little bit confused !! 我有点困惑!! why the program stops exeuction when the count is equal to 4716 !! 为什么程序在计数等于4716时停止执行!! PS: Executed under Visual studio 2010. thanks PS:在Visual Studio 2010下执行。谢谢

The limit of recursive calls depends on the size of the stack. 递归调用的限制取决于堆栈的大小。 The C++ language is not limiting this (from memory, there is a lower limit of how many function calls a standards conforming compiler will need to support, and it's a pretty small value). C ++语言不限制这一点(从内存来看,符合标准的编译器需要支持多少函数调用的下限,这是一个非常小的值)。

And yes, recursing "infinitely" will stop at some point or another. 是的,“无限”递归会在某个时刻停止。 I'm not entirely sure what else you expect. 我不完全确定你还有什么期望。

It is worth noting that designing software to do "boundless" recursion (or recursion that runs in to the hundreds or thousands) is a very bad idea. 值得注意的是,设计软件进行“无限”递归(或者运行成数百或数千次的递归)是一个非常糟糕的主意。 There is no (standard) way to find out the limit of the stack, and you can't recover from a stack overflow crash. 没有(标准)方法来找出堆栈的限制,并且无法从堆栈溢出崩溃中恢复。

You will also find that if you add an array or some other data structure [and use it, so it doesn't get optimized out], the recursion limit goes lower, because each stack-frame uses more space on the stack. 您还会发现,如果添加一个数组或其他数据结构[并使用它,因此它不会被优化],递归限制会降低,因为每个堆栈帧在堆栈上使用更多空间。

Edit: I actually would expect a higher limit, I suspect you are compiling your code in debug mode. 编辑:我实际上会期望更高的限制,我怀疑你是在调试模式下编译代码。 If you compile it in release mode, I expect you get several thousand more, possibly even endless, because the compiler converts your tail-recursion into a loop. 如果你在发布模式下编译它,我希望你得到几千个,甚至可能无穷无尽,因为编译器会将你的尾递归转换为循环。

The stack size is dependent on your environment. 堆栈大小取决于您的环境。

In *NIX for instance, you can modify the stack size in the environment, then run your program and the result will be different. 例如,在* NIX中,您可以修改环境中的堆栈大小,然后运行程序,结果将不同。

In Windows, you can change it this way (source) : 在Windows中,您可以这样更改它(源)

$ editbin /STACK:reserve[,commit] program.exe

You've probably run out of stack space. 你可能已经没有堆栈空间了。

Every time you call the recursive function, it needs to push a return address on the stack so it knows where to return to after the function call. 每次调用递归函数时,它都需要在堆栈上推送一个返回地址,以便它知道在函数调用之后返回的位置。

It crashes at 4716 because it just happens to run out of stack space after about 4716 iterations. 它在4716崩溃,因为它恰好在大约4716次迭代后耗尽了堆栈空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM