- #1
petrushkagoogol
- 28
- 4
Upto how many levels of recursion can be used in an algorithm to avoid stack overflow error ?
petrushkagoogol said:Upto how many levels of recursion can be used in an algorithm to avoid stack overflow error ?
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.Irene Kaminkowa said:It depends.
For example, tail recursion is as good as any iteration.
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.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, ...).Irene Kaminkowa said:It depends.
A compiler (interpreter) may treat tail-recursive calls as jumps not function calls. Then, the space is constant.
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: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.
unsigned factorial (unsigned n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n-1);
}
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);
}
Consider GCD - it can be implemented as tail recursion.Mark44 said:The compiler would still have to push the parameters onto the stack
I just showed one for factorial. It can be implemented as tail recursion.Irene Kaminkowa said:Consider GCD - it can be implemented as tail recursion.
Factorial - cannot.
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.
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.
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.
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.
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.