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.
stack overflowAddressSanitizer: stack-overflowStack overflow (C00000FD)Process is terminating due to StackOverflowExceptionEXC_BAD_ACCESS (code=2 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
- Fix or add the base case, and test depth with small inputs
- Allocate big buffers with
std::vectorornew(heap) instead of on the stack - Convert deep recursion to a loop with an explicit stack
Detailed fix by platform
Linux
- Check the limit:
ulimit -s(kB); raise it for a test withulimit -s unlimited - In gdb,
bt 20shows the same frames repeating
Visual Studio
- Project properties → Linker → System → Stack Reserve Size, or
/STACK:8388608; the real fix is to remove the recursion
Code examples
Heap instead of stack
#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
- Backtrace — Does the same function repeat?
- Size — Any large local arrays?
- 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:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026