What a stack overflow is
A stack overflow is an error that happens when a program tries to use more space on its 'call stack' than the stack has available. The call stack is a region of memory that keeps track of the functions a program is currently running. It has a limited size, and when a program pushes too much onto it, the stack overflows its boundary, and the program typically crashes. It is a common and recognizable type of runtime error.
What the call stack is
When a program calls a function, it adds an entry to the call stack holding information about that function - things like its local data and where to return when it finishes. As functions call other functions, entries pile up; as functions finish, entries are removed. This stack lets the program keep track of the chain of function calls. Because it occupies a fixed, limited amount of memory, it can only hold so many entries at once.
The usual cause: runaway recursion
The most common cause of a stack overflow is recursion that does not stop. Recursion is when a function calls itself, and it is a useful technique - but each call adds another entry to the stack. If a recursive function lacks a proper stopping condition, or the condition is never met, it keeps calling itself, piling entry after entry onto the stack until the space runs out. This runaway recursion quickly exhausts the stack and triggers the overflow.
Other causes
While infinite recursion is the classic cause, a stack overflow can also occur from very deep but finite recursion that simply needs more stack than is available, or from allocating very large amounts of data on the stack at once. In all cases, the root issue is the same: the program's demand for stack space exceeds the limited amount the system has set aside, so the stack cannot grow further and the program fails.
Avoiding stack overflows
The key to avoiding stack overflows is making sure recursion always has a correct base case that stops it, and being mindful of how deep recursion can go. For problems that require very deep repetition, rewriting recursion as a loop, which does not consume stack space per iteration, can help. Being careful about putting large data structures on the stack rather than elsewhere in memory also reduces the risk of overflowing it.
Why it matters
The stack overflow is a classic programming error that illustrates how the call stack works and why its limits matter. Understanding what a stack overflow is - exceeding the limited memory of the call stack, usually through runaway recursion - helps programmers write recursive code safely and debug crashes. For anyone learning to program, grasping the stack and its overflow is a genuinely useful step.
Related on Skillo
See also: What is a bug? Software bugs and debugging explained, What is a data structure? Explained simply.
Sources
Published date reflects the original event date (2023-09-05). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Leave a Reply