Stack Overflow
This function resembles a Fibonacci recurrence, but lacks a base case.
void fibonacci(int a, int b) {
int c;
c = a + b;
fibonacci(b,c);
}
Because of the infinite recursion, each call consumes stack space until the program crashes with a stack overflow.
High Address
+-----------------------------+
| |
|.............................|
| b | // 1
|.............................|
| a | // 0
|.............................|
| Return Address |
|.............................|
| Frame Pointer #1 |
|.............................|
| c | // 1
|.............................|
| b | // 1
|.............................|
| a | // 1
|.............................|
| Return Address |
|.............................|
| Frame Pointer #2 |
|.............................| <- Frame Pointer #3
| c | // 2
|.............................|
| |
| ... |
| |
|.............................|
Low Address
The stack is limited by the operating system, not by C itself.
Typical defaults:
- Linux: ~8 MB (often configurable via ulimit -s)
- Windows: ~1 MB (default)
- Embedded systems: much smaller (KBs)
High Address
+-----------------------------+
| Caller's data |
|.............................|
| b | // 1
|.............................|
| a | // 0
|.............................|
| Return Address |
|.............................|
| Frame Pointer #1 |
|.............................|
| c | // 1
|.............................|
| b | // 1
|.............................|
| a | // 1
|.............................|
| Return Address |
|.............................|
| Frame Pointer #2 |
|.............................| <- Frame Pointer #3
| c | // 2
|.............................|
| |
| ... |
| |
|.............................|
| b | // 301789297
|.............................|
| a | // -1586439200
|.............................|
| Return Address |
|.............................|
| Frame Pointer #n-2 |
|.............................| <- Frame Pointer #n-1
| c | // -1284649903
|.............................|
| b | // -1284649903
|.............................|
| a | // 301789297
|.............................|
| Return Address |
|.............................|
| Frame Pointer #n-1 |
|.............................| <- Frame Pointer #n
| c | // -982860606
+-----------------------------+ <- End of the Stack
Low Address
Once that memory is exhausted, the stack cannot grow anymore.
When the stack exceeds its allowed region, the function call tries to write past the stack boundary where the OS detects an invalid memory access and terminates the process.
Common symptoms:
- Segmentation fault (Linux/macOS)
- Stack overflow exception (Windows)
- Program aborts or crashes silently (embedded systems)