Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top