简体   繁体   English

堆栈形式会像递归一样导致溢出,但是会更晚吗?

[英]Will stack form cause overflow just like recursion, but much later?

If I converted a very deep recursion into a stack form, am I pretty much guaranteed to avoid stack overflow? 如果我将很深的递归转换为堆栈形式,是否可以保证避免堆栈溢出? That is, does the Stack go on the stack or the heap? 也就是说,堆栈是放在堆栈还是堆上?

Previous recursion was going down to 5 million steps then stack overflow was thrown. 以前的递归下降到500万步,然后引发了堆栈溢出。

Yes. 是。 From now on you'll have to worry about out of memory errors instead. 从现在开始,您将不得不担心内存不足错误。

Usually a programming language runtime where the stack grows in recursion either from the functions not being in tail position or the runtime not having tail recursion optimization the amount of memory reserved for stack is small compared to total available memory for the runtime. 通常,在编程语言运行时中,堆栈要么由于未处于尾位置的函数而进行递归增长,要么由于未进行尾递归优化的运行时而增长,因此与该运行时可用的总内存相比,为堆栈保留的内存量很小。 When not using the programming languages stack you use the heap memory and you usually can go a lot deeper since there is more of it. 当不使用编程语言堆栈时,您将使用堆内存,并且通常会更深入,因为其中有更多的内存。

However when you have used up all the memory the OS is willing to give you (ie. not reserved) you'll no longer be able to allocate more memory and that random operation that requires more memory will fail. 但是,当您用完所有内存后,操作系统会愿意(即不保留)为您分配更多的内存,并且需要更多内存的随机操作将失败。

Of course these puny physical limitations does not apply to theoretical computing. 当然,这些微不足道的物理限制不适用于理论计算。 Ie. 就是 A Turing Machine will never ever fail because there is no more memory since the tape is infinitely long. 图灵机永远不会失败,因为磁带无限长,因此没有更多的内存。

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

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