Recursion: Avoiding Stack Overflow Errors

  • Context:
  • Thread starter Thread starter petrushkagoogol
  • Start date Start date
  • Tags Tags
    Indefinite Recursion
Click For Summary

Discussion Overview

The discussion revolves around the limits of recursion in algorithms and strategies to avoid stack overflow errors. Participants explore various aspects of recursion, including its implementation, the impact of programming languages and compilers, and the conditions under which recursion can be safely used.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants note that the maximum levels of recursion depend on stack space and the space each call consumes.
  • There is a suggestion that iterative algorithms may be preferable to recursion to avoid stack overflow.
  • Questions are raised about the type of recursion, programming environment, and operating system affecting recursion limits.
  • Some argue that tail recursion can be as efficient as iteration, while others challenge this by discussing stack usage in recursive calls.
  • Participants mention that compilers may optimize tail recursion, potentially reducing stack space usage, but this is not universally applicable across all languages.
  • Examples of recursive functions, such as factorial and GCD, are discussed to illustrate the differences between regular and tail recursion.
  • There is a mention of the historical context of compiler optimizations and how it influences current understanding of recursion.
  • One participant suggests that if stack overflow is approached, it may indicate an inappropriate algorithm choice.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of tail recursion compared to iteration, the role of compiler optimizations, and the implications of recursion in various programming contexts. No consensus is reached regarding the best practices for recursion limits.

Contextual Notes

Limitations include the dependence on specific programming languages and environments, as well as the variability in compiler behavior regarding tail call optimization. The discussion does not resolve the complexities surrounding these factors.

petrushkagoogol
Messages
28
Reaction score
4
Upto how many levels of recursion can be used in an algorithm to avoid stack overflow error ?
 
Technology news on Phys.org
There is no guarantees as it depends upon:
1. How much stack space is there to begin with?
2. How much space does each call take?

While you can measure these for a particular use case, in general you can't rely on this.

For any of the products I work on, I try to avoid using recursion and use an iterative algorithm. When I do use recursion, it is either for an internal tool that customers won't have to use or has some means of guaranteeing it won't require a deep stack.
 
  • Like
Likes   Reactions: Fooality and Pepper Mint
petrushkagoogol said:
Upto how many levels of recursion can be used in an algorithm to avoid stack overflow error ?

This question could not be more abstract. What kind of recursion, which programming environment / language and last but not least which operating system?
 
It depends.
For example, tail recursion is as good as any iteration.
 
Irene Kaminkowa said:
It depends.
For example, tail recursion is as good as any iteration.
I don't think so. In recursion of any kind, the return address and any parameters have to be pushed onto the stack, something that is not done when a loop executes. Eventually the program will run out of available stack storage.
 
  • Like
Likes   Reactions: QuantumQuest
It depends.
A compiler (interpreter) may treat tail-recursive calls as jumps not function calls. Then, the space is constant.
 
Irene Kaminkowa said:
It depends.
A compiler (interpreter) may treat tail-recursive calls as jumps not function calls. Then, the space is constant.
If the recursive function takes parameters, I don't see how a compiler could rewrite a function call as a jump. The compiler would still have to push the parameters onto the stack.
 
  • Like
Likes   Reactions: QuantumQuest
Irene Kaminkowa said:
It depends.
A compiler (interpreter) may treat tail-recursive calls as jumps not function calls. Then, the space is constant.
Exactly. Many compilers are very good at recognizing tail recursion. It's called "Tail call optimization". Some interpreters are also good at recognizing tail recursion (e.g., many dialects of Lisp), many others are not (python (Guido hates TCO), perl, ruby, ...).
 
Mark44 said:
If the recursive function takes parameters, I don't see how a compiler could rewrite a function call as a jump. The compiler would still have to push the parameters onto the stack.
Tail recursion is a very special kind of recursion. The following C function to compute n! is recursive but it is not tail recursive:
C:
unsigned factorial (unsigned n) {
    if (n <= 1) {
        return 1;
    } else {
        return n * factorial(n-1);
    }
It appears to be tail recursive because the last function that is called is the function itself. However, the last operation is to multiply the value returned value by that call with n. One needs to write a helper function to make this tail recursive in C:
C:
unsigned factorial_helper (unsigned n, unsigned prod) {
    if (n <= 1) {
        return prod;
    } else {
        return factorial_helper(n-1, n*prod);
    }
}

unsigned factorial (unsigned n) {
    return factorial_helper(n, 1);
}

Functional languages invite one to write tail recursive functions in lieu of loops. Non-functional languages such as C (and everything derived from C) invite one to write loops in lieu of tail recursive functions.
 
  • Like
Likes   Reactions: FactChecker, Pepper Mint and Irene Kaminkowa
  • #10
Mark44 said:
The compiler would still have to push the parameters onto the stack
Consider GCD - it can be implemented as tail recursion.
Factorial - cannot.

There is an article "Tail call" in Wiki on this subject.
 
  • #11
Irene Kaminkowa said:
Consider GCD - it can be implemented as tail recursion.
Factorial - cannot.
I just showed one for factorial. It can be implemented as tail recursion.

Ackermann's function cannot.
 
  • #12
Good point. My mindset is from learning C 30+ years ago, before there were such sophisticated optimization schemes.
 
  • #13
Perhaps, there is a toggle for each recursion.
Mathematicians have not proved the opposite so far.
So, there is hope. And a scope for work.
 
  • #14
Usually if you even come close to a stack overflow, you are using the wrong algorithm. What kind of algorithm are you doing?

Also, why is everyone talking about compiler optimization? The code has to work while he's debugging it too, without the compiler optimizations.
 
  • Like
Likes   Reactions: petrushkagoogol

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 25 ·
Replies
25
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
13K
  • · Replies 1 ·
Replies
1
Views
9K