Eli5-What does a “stack overflow” mean?

1.18K views

Eli5-What does a “stack overflow” mean?

In: 847

41 Answers

Anonymous 0 Comments

A portion of the RAM on a computer is reserved for the “stack” which is for data that is very transient, such as the data passed to a function in a program as arguments. In more “user friendly” language like python, the programmer doesn’t need to be aware of this or think about it, but in languages like C and C++ when you create a variable, it goes to the stack unless you explicitly create it in a way that it is stored in the rest of the non-stack RAM. If your program tries to put too much data into the stack and it fills up the space that was reserved for it, then it is called a stack overflow.

Anonymous 0 Comments

The memory stack of a program is limited in size by default. So if you throw too much data in there, like function parameters you call, then you can run out of space.

This is very common if you get function calls in a loop or recursion when you don’t exit properly and the stack fills up its memory until it default (or configured) stack size is exceeded.

Things are pushed into the stack, like function calls and parameters, and popped off the stack, like function return values. Pushing too much stuff on runs out of space.

If you need bigger things in more space you allocate it to the heap.

For example. 16 bit integer (2 bytes) might be on the stack, but if you have a long string, which is 1gb in size, that would be allocated on the heap, and that heap’s memory address would be on the stack.

This is relevant to when you pass arguments to functions by value (on the stack) such as the number “7” or by reference (to the heap) such as text or a binary image and pass only the memory address.

Anonymous 0 Comments

I’m a dad (operating system) and I am giving my son (a program) a cup for him to do whatever he wants with it. The cup is a container for anything really (“Memory”). My son can fill up the cup, mix liquids together, blow bubbles, drink – whatever. What he can’t do is put more liquid in the cup than the cup can hold – it will overflow and spill all over my carpet. When that happens, I take away the cup because he is a risk for spilling. In the same way you monitor your child to make sure he doesn’t overflow the bathtub or a cup, your OS is monitoring to make sure a program doesn’t go “out of bounds” with its resources. If it does, stop the program from doing what its doing and no more resources. There are different types of resources (stack vs heap) but this is my “5” answer and the others here do a good job explaining more in detail.

Anonymous 0 Comments

The simplest computer programs are just a series of instructions executed one after the other. Start with the first one, and keep going until you reach the last one, at which point the program ends. But that doesn’t usually lead to very interesting programs.

Now, you can write loops to do more interesting things (generally, things that tend to repeat a lot) but even with that, you’ll eventually reach a wall where you can’t do anything more interesting or complex.

One way to do more interesting or complex things is to create subroutines (sometimes also known as functions.) Basically, you write a smaller program which takes some parameters as input, and does things. If it also returns a value back, it’s a function. But now, you have a tool that you can “call” on at any other point in your program. Control goes from your main program to the subroutine until that subroutine ends, at which point the program flow automatically returns to the point where the subroutine was called.

In order to pass those parameters to your subroutine, and remember where to come back to once the subroutine ends, your computer uses a stack: it puts the parameters and the return address in a special place in memory, and then starts executing the subroutine’s code. The subroutine accesses those parameters, and once it ends, the code jumps back to the return address.

So, if all you did was call subroutines from your main code, and then return back to the main code, you wouldn’t need a stack, you would just need one spot in memory at a time. But you see, one subroutine can call another subroutine. Which can call another subroutine. And so on. So every time one of those subroutines ends, your computer needs to go back to where it was called, but then if that was in another subroutine, it needs to remember its return address, too. So you need a way to remember all those return addresses!

So that’s why we use a stack. It’s called a stack because it kind of works like those stacks of plates you might have seen at buffet restaurants, where every plate you put on the stack pushes the stack down, and every time you take out a plate, the stack comes back up. All you can do is put a plate on top of the stack (hiding the plate under it) or pop the top plate off (revealing the next plate under it.)

So, now that you understand what’s a stack, and how it’s used, here’s where a problem can happen. If one subroutine can call another one, and then that one can call another one, and so on, that stack can actually pile up pretty high! going back to that stack of plates at the buffet, there’s only so many plates that can fit, and you can reach a point where no more plates will fit. If you try to put another plate on top, the stack won’t go down anymore. It will OVERFLOW!

Now, if all you have is subroutine A calling subroutine B, which calls subroutine C, and so on, you’re unlikely to overflow, because you’re writing each new subroutine. You know how many plates you’re putting on the stack. The problem comes when you get something like subroutine C calling subroutine A. Put another way, if you have A > B > C > A, well, you already know that A calls B, which calls C, so now your stack is A>B>C>A>B>C>A and if there’s nothing to break that chain, these calls will keep going, putting a new plate on top of the stack at every call, until no more plates fit. Stack Overflow!

Now, there’s legitimate reasons to have call sequences like that. You might even have sequences like A>A>A>A… and as long as you have a condition in there somehwere that will stop A from calling itself, you’re probably safe. But if you made a mistake and there’s a way for A to keep calling itself, your stack will eventually overflow!

By the way, subroutines and functions that call themselves (either directly as A>A>A>A>… or indirectly like A>B>C>A>B>C>A>…) are called “recursive”, and stack overflows are common when people make mistakes with such subroutines and functions.

Anonymous 0 Comments

When your mom asks you to do five chores but you can only remember four, then later that day when doing homework you can only think of the fifth chore.

Anonymous 0 Comments

Stack is a data structure. Think like a piles of dishes, you can only add more plates to the top, or remove the top plate (we call it LIFO: Last In, First Out), but in this case, the dishes are actually data in the computer memory.

In theory, we could add as many data to a stack as we want, but memory is finite, so we set a limit to the size of the “pile of data”. When you try to add data to a stack that is full, a stack overflow happens. Depending on the implementation of the stack, either the program will prevent more data from being added, or the data will be added to an unexpected location of memory, leading to all sorts of unexpected problems.

Anonymous 0 Comments

You can think of it like an actual stack of index cards. Let’s say you’re doing some really hard math problem. You might want to do a small chunk of it separately to simplify things, so you grab a new index card, stack it on top of the first one, and do your smaller calculation there. From there, you can copy the result back over to the index card on the bottom. Of course, that second index card of calculations can also be really complex and require an index card of its own. Stack overflow errors happen when you run out of space to stack more index cards onto the pile.

Anonymous 0 Comments

When a program jumps from one section of code to another, it might have to save some information, (like where to come back to) in memory (RAM). If this code jumps to another bit of code, it may have to save more information, too. This data is sequential in memory, much like a stack of boxes, hence the name.

Typically, the code should not jump too many times, so there is usually no problem. But if there was an error in the writing of the program, there might be so many jumps that the stack uses up all the available memory. This is an error message a programmer would see frequently while writing code, and know what to do to fix it. It just became a cool name for a website for fixing code, because everyone using that site would have seen it before.

Anonymous 0 Comments

When you code a computer program, you need to use functions (to perform small tasks). Those functions need to use the stack – a special part of the memory of the computer.

If you use too many functions at the same time, the stack becomes full and you cannot use more functions -> we call this a **stack overflow**.

Anonymous 0 Comments

The “stack” is a section of memory reserved for use by a computer’s CPU to temporarily store working data. It’s called a stack because it operates like a stack (of plates, memos, whatever). You can only add data by tossing it on top of the stack, and you can only read data by taking it off the top of the stack.

But the memory allocated to the stack is limited. When an attempt is made to add more data to the stack than it has room for, *voila,* “stack overflow”. This is a critical error and the program stops immediately.

The easiest way to get a stack overflow is for a subroutine to call itself. When a program calls a subroutine, it pushes a return address onto the stack so the subroutine knows where to return to when it’s done. But if it’s calling itself and never returning, those return addresses will pile up infinitely. Without the enforced size limit on the stack, it could grow until it overwrites all memory, crashing the entire computer instead of just the currently running program.