限制C ++中的递归调用(约5000)? [英] Limit recursive calls in C++ (about 5000)?

查看:80
本文介绍了限制C ++中的递归调用(约5000)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了知道C ++中递归调用的限制,我试过这个函数!

In order to know the limit of the recursive calls in C++ i tried this function !

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 );
}

当计数等于4716时,此程序停止!所以限制只是4716!
我有点困惑!为什么程序停止运行时,计数等于4716!
PS:在Visual studio 2010下执行。
感谢

this program halt when count is equal 4716 ! so the limit is just 4716 !! I'm a little bit confused !! why the program stops exeuction when the count is equal to 4716 !! PS: Executed under Visual studio 2010. thanks

推荐答案

递归调用的限制取决于堆栈的大小。 C ++语言不限制这一点(从内存,有一个下限,有多少函数调用符合标准的编译器将需要支持,它是一个很小的值)。

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).

是的,递归无限将停止在某个点或另一个。我不完全确定你期望什么。

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.

编辑:我实际上会期望更高的限制,我怀疑你是在调试模式下编译您的代码。如果你在发布模式下编译它,我希望你得到几千个,甚至可能是无尽的,因为编译器将你的尾递归转换成一个循环。

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.

这篇关于限制C ++中的递归调用(约5000)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆