Avoiding Stack Overflow
Keep recursion bounded.
What Is A Stack Overflow?
The call stack has a limited size. Each function call uses part of it for parameters and locals.
If recursion goes too deep, the stack fills up and the program crashes with a stack overflow.
Missing Base Case
The most common cause is a base case that is never reached. This loops forever and overflows the stack.
Do not run this kind of function; study why it fails.
int broken(int n) {
/* no base case: never stops */
return broken(n + 1);
}All lessons in this course
- How Recursion Works
- Classic Recursive Problems
- Recursion vs Iteration
- Avoiding Stack Overflow