0Pricing
C Academy · Lesson

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

  1. How Recursion Works
  2. Classic Recursive Problems
  3. Recursion vs Iteration
  4. Avoiding Stack Overflow
← Back to C Academy