If you've written code, or spent time around people who do, you've heard of Stack Overflow, the website programmers turn to when they're stuck. But the name isn't random. It comes from a very real error that trips up programmers at every skill level. Here's what a stack overflow actually is, what it looks like in different languages, how to fix one, and how the website ended up named after it.
What Is a Stack?
In programming, a stack is a way of organizing data that works like a pile of plates. You add plates to the top and take them off the top. The last one you put on is the first one you take off. Computer scientists call this LIFO: last in, first out.
Every running program has a special one called the call stack. Each time your code calls a function, the program pushes a stack frame onto it: a little record holding where the function was called from, the values passed into it, and its local variables. When the function finishes, its frame is popped off and the program picks up right where it left off.
Here's what the call stack looks like when main calls load_page, which calls fetch_data:
+------------------+ <- top: currently running | fetch_data() | +------------------+ | load_page() | +------------------+ | main() | +------------------+ <- bottom: where the program started
When fetch_data finishes, its frame comes off the top and load_page continues. That's how functions can call other functions, which call other functions, and still all return to the right place in the right order. The call stack is the program's way of remembering where it is.
So What Is a Stack Overflow?
The call stack isn't infinite. Each program gets a fixed amount of memory for it. On Linux, a program's main stack is typically capped at 8 MB. On Windows, the default is just 1 MB. Extra threads often get even less.
A stack overflow happens when the call stack runs out of room. Each frame is small, but if they keep piling up without ever coming off, the stack eventually fills, and the program can't make another function call.
By far the most common cause is runaway recursion. Recursion means a function that calls itself. It's a perfectly good technique, as long as there's a condition that eventually makes it stop. Here's the classic mistake in Python, a countdown that never checks when to stop:
def countdown(n):
print(n)
countdown(n - 1) # no stopping point
countdown(5)It prints 5, 4, 3, 2, 1, 0, -1, -2 and keeps going. Every call adds another frame to the stack, none of them ever finish, and eventually the stack is full. The fix is a base case, a condition that ends the recursion:
def countdown(n):
if n < 0: # base case: stop here
return
print(n)
countdown(n - 1)What a Stack Overflow Looks Like in Each Language
| Language | What you'll see |
|---|---|
| Python | RecursionError: maximum recursion depth exceeded. Python stops recursion at about 1,000 calls deep by default, before the real stack runs out. |
| JavaScript | RangeError: Maximum call stack size exceeded |
| Java | java.lang.StackOverflowError |
| C# and .NET | The program simply crashes. A stack overflow can't be caught there, so the process is terminated. |
| C and C++ | Usually a crash. On Linux that's typically a "Segmentation fault." On Windows, you'll see exception code 0xC00000FD, which is literally named STATUS_STACK_OVERFLOW. |

You can check Python's limit yourself:
import sys
print(sys.getrecursionlimit()) # usually 1000It's Not Always an Infinite Loop
Sometimes the code is technically correct and the recursion is just too deep. Processing a deeply nested folder structure, walking a huge tree of data, or running a recursive algorithm on a big input can legitimately need tens of thousands of nested calls. That blows past the limit even though the logic would eventually finish.
A few other causes worth knowing:
- Two functions calling each other forever, called mutual recursion. Function A calls B, B calls A, and round and round they go.
- A base case that never gets reached. Counting down by 2 from an odd number while checking for exactly 0 skips right past it.
- Accidental recursion, like a property or method that calls itself by mistake. A setter that assigns to its own property name is a classic one in object-oriented languages.
- Huge local variables. In languages like C, declaring a giant array inside a function puts it on the stack, and a big enough one can overflow it in a single call.
How to Debug a Stack Overflow
The error message is usually your best friend here. A Python stack overflow ends with a traceback like this:
File "app.py", line 3, in countdown
countdown(n - 1)
File "app.py", line 3, in countdown
countdown(n - 1)
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceededThe same function and line, repeated hundreds of times, tells you exactly where the loop is. From there:
- Find the repeating function in the stack trace. If two functions alternate, that's mutual recursion.
- Check the base case. Make sure it exists, and that every call actually moves closer to it.
- Print the input on each call for a few calls. You'll quickly see whether it's heading toward the stopping point or away from it.
- Try a tiny input. If it fails with an input of 3, it's a logic bug. If it only fails with an input of 100,000, the recursion is simply too deep and needs rewriting.
How to Fix a Stack Overflow
1. Fix the base case
For most bugs, this is the whole fix. Add the missing stopping condition, or correct the one that never triggers.
2. Rewrite the recursion as a loop
Almost any recursive function can be rewritten with a regular loop, which uses no extra stack per step. Here's a recursive factorial and the same thing as a loop:
# Recursive: one stack frame per number
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
# Loop: same answer, no stack growth
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return resultThe loop version handles factorial(5000) without complaint. The recursive version hits Python's limit long before that.
For trickier cases, like walking a tree, you can replace the call stack with your own list that you push and pop items from. Same idea as recursion, but the list lives in regular memory, which has far more room.
3. Raise the limit, carefully
Python lets you raise its recursion limit with sys.setrecursionlimit(), and many languages let you increase the stack size for a thread. It's a band-aid, though. In Python, setting it too high can crash the entire interpreter instead of giving you a clean error.
A note on tail recursion
Some languages, especially functional ones like Scheme, can run a certain kind of recursion without growing the stack at all. If the recursive call is the very last thing a function does (a tail call), the language can reuse the current frame instead of adding a new one. Python and Java don't do this, so in those languages you still need a loop.
Stack Overflow vs Stack Buffer Overflow
These two sound almost identical, but they're very different, and mixing them up is common.
- A stack overflow is the stack running out of space, like the examples above. It crashes the program, but on its own it's a bug, not a security hole.
- A stack buffer overflow is a program writing past the end of a variable stored on the stack, such as copying a 500-character string into space meant for 100. The extra data spills over and overwrites nearby memory, sometimes including the address the program will jump back to next. That's the basis of one of the oldest and most famous hacking techniques. It mostly affects languages like C and C++, which don't check those boundaries automatically, and modern systems add several layers of protection against it.
Same neighborhood in memory, completely different problem.

How the Website Got Its Name
Stack Overflow, the website, was founded in 2008 by Jeff Atwood and Joel Spolsky. Before it, programming help was scattered across forums, mailing lists and paywalled answer sites, and getting a good answer to a specific question could take days.
Their idea was a free question-and-answer site where the best answers got voted to the top and helpful people earned reputation points. The name, picked with help from Atwood's blog readers, was an inside joke: the stack overflow is one of the most universally recognizable bugs in programming, and needing help is the most universal programmer experience of all.
It worked spectacularly. For more than a decade, pasting an error message into a search engine almost guaranteed a Stack Overflow result. The same model spread to the wider Stack Exchange network, with sites for everything from math to cooking to personal finance. In 2021, the company was sold to tech investor Prosus for $1.8 billion.
Stack Overflow in the AI Era
The website's story has taken a sharp turn. New questions peaked at more than 200,000 a month around 2014 and drifted down for years after. Then AI chat tools arrived in late 2022 and the drop turned into a cliff. By May 2025, the site was on pace for only about 14,000 new questions in a month, roughly where it was in its earliest days.
The irony is that those AI tools learned a lot of what they know from Stack Overflow's millions of human-written answers. The archive is still enormously useful, and it still shows up in search results constantly. But "just ask Stack Overflow" isn't the reflex it used to be.
Frequently Asked Questions
Can a stack overflow damage my computer?
No. It crashes or stops the program that caused it, and that's it. The operating system cleans up the memory, and nothing else is affected.
Is recursion bad?
Not at all. Recursion is often the clearest way to solve problems like walking through folders or nested data. It just needs a solid base case, and for very deep inputs, a loop may be the safer choice.
What's the difference between the stack and the heap?
The stack holds function calls and their small local variables, and it's fast but limited in size. The heap is a much larger pool of memory for data that needs to stick around or grow, like big lists and objects. Running out of stack causes a stack overflow. Running out of heap causes an out-of-memory error.
What's a stack trace?
It's a snapshot of the call stack at the moment an error happened, listing every function call that led there. It's the single most useful clue for debugging almost any crash, not just stack overflows. Read it from the bottom for the error and from the top for where things started.
TL;DR
A stack overflow happens when a program's call stack runs out of space, almost always because a function keeps calling itself without a proper stopping point. Read the stack trace to find the repeating function, then fix the base case, rewrite the recursion as a loop, or, as a last resort, raise the limit. It's not the same thing as a stack buffer overflow, which is a security problem. And Stack Overflow, the website, was named after the error as a nod to the one thing every programmer has in common: getting stuck and needing help.

