Recursion: Avoiding Stack Overflow Errors

In summary, while there is no guarantees, using less recursion generally results in less stack overflow errors.
  • #1
petrushkagoogol
28
4
Upto how many levels of recursion can be used in an algorithm to avoid stack overflow error ?
 
Technology news on Phys.org
  • #2
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 Fooality and Pepper Mint
  • #3
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?
 
  • #4
It depends.
For example, tail recursion is as good as any iteration.
 
  • #5
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 QuantumQuest
  • #6
It depends.
A compiler (interpreter) may treat tail-recursive calls as jumps not function calls. Then, the space is constant.
 
  • #7
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 QuantumQuest
  • #8
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, ...).
 
  • #9
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 [itex]n![/itex] 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 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 petrushkagoogol

1. What is recursion and why is it important to avoid stack overflow errors?

Recursion is a programming concept in which a function calls itself until a certain condition is met. It is important to avoid stack overflow errors because they occur when the call stack, which keeps track of function calls, becomes too large and causes the program to crash.

2. How can I detect and prevent stack overflow errors when using recursion?

You can detect and prevent stack overflow errors by setting a base case in your recursive function, which will stop the function from calling itself when a certain condition is met. You can also limit the number of recursive calls or use tail recursion, which is a technique that optimizes recursive calls to avoid stack overflow errors.

3. Are there any alternatives to recursion to avoid stack overflow errors?

Yes, there are alternative methods such as iteration, which involves using loops instead of recursive calls. However, recursion can be a more elegant and efficient solution for certain problems, so it is important to understand how to use it properly to avoid stack overflow errors.

4. Can I use recursion in any programming language?

Yes, recursion is a universal concept and can be used in any programming language that supports functions and conditional statements. However, the implementation and syntax may vary between languages.

5. What are some common mistakes to avoid when using recursion?

Some common mistakes to avoid when using recursion include not setting a base case, causing an infinite loop, and not understanding the logic of the recursive function. It is important to carefully plan and test your recursive functions to avoid these errors.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
2
Views
323
  • General Math
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
636
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
951
  • Programming and Computer Science
Replies
25
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top