stack overflow C/C++ Origin: Application & API Last updated 15 Oct 2026

C/C++: stack overflow — infinite recursion or huge local array (Segmentation fault, 0xC00000FD)

The call stack ran out of space, typically from recursion that never stops or from a very large array declared as a local variable.

Exact message you may see stack overflowAddressSanitizer: stack-overflowStack overflow (C00000FD)Process is terminating due to StackOverflowExceptionEXC_BAD_ACCESS (code=2
Seen on: C/C++ Linux Windows

Meaning

Each function call uses stack memory (typically 8 MB on Linux, 1 MB on Windows). Unbounded recursion, deep but legitimate recursion on big inputs, and arrays like int a[10'000'000]; inside a function exceed it. On Linux the process dies with a plain segmentation fault; on Windows you see 0xC00000FD.

Common causes

  • Recursive function without a reachable base case
  • Recursion depth proportional to input size (linked list, DFS on large graphs)
  • Large arrays or objects declared as local variables
  • Mutual recursion between functions or constructors/destructors
  • Copy constructor that calls itself

⚡ Quick fix

  1. Fix or add the base case, and test depth with small inputs
  2. Allocate big buffers with std::vector or new (heap) instead of on the stack
  3. Convert deep recursion to a loop with an explicit stack

Detailed fix by platform

Linux

  1. Check the limit: ulimit -s (kB); raise it for a test with ulimit -s unlimited
  2. In gdb, bt 20 shows the same frames repeating

Visual Studio

  1. Project properties → Linker → System → Stack Reserve Size, or /STACK:8388608; the real fix is to remove the recursion

Code examples

Heap instead of stack

cpp
#include <vector>

long long sum_to(long long n) {              // infinite if called with n < 0
    if (n <= 0) return 0;                    // base case
    return n + sum_to(n - 1);
}

int main() {
    // int big[5'000'000];                   // 20 MB on the stack: crashes
    std::vector<int> big(5'000'000);         // heap: fine
    return static_cast<int>(sum_to(1000) % 256);
}

How to diagnose

  1. Backtrace — Does the same function repeat?
  2. Size — Any large local arrays?
  3. Input — How deep does the recursion get?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Bus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errordouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…Floating point exception — C/C++: Floating point exception (core dumped) — SIGFPE, usually integer division by zero (C/C++)Floating point except…LeakSanitizer: detected memory leaks — C/C++: ==ERROR: LeakSanitizer: detected memory leaks — Direct leak of N byte(s) (C/C++)LeakSanitizer: detect…Segmentation fault — C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer (C/C++)Segmentation faultstack smashing detected — C/C++: *** stack smashing detected ***: terminated — buffer overflow on the stack (C/C++)stack smashing detect…stack ove…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.