Imagine you’re telling someone a story like my grandmother.
“The other day I went to the super market and you’ll never believe who I saw! I saw Gertrude! She was near the tomatoes. And she…”
Before you can continue, you want to mention a side-story
“… she — well, first I saw the price of the tomatoes. 50 cents each!! I can’t believe how bad the economy is doing. Did you see the price of…”
And once again you get side-railed.
“… of — well, let me just tell you, if your grandfather were still alive he would have had some CHOICE WORDS to say to our politicians, I’ll tell you that!”
… And in the same way you just keep getting sidetracked with the side-stories. Now, in a perfect world, this is how your entire discussion is structured:
a. Start talk about supermarket
-b. Start talk about Gertrude
–c. Start talk about tomatoes
—d. Start talk about economy
—-e. Start talk about politicians
—–f. Start talk about grandfather
—–g. End talk about grandfather
—-h. End talk about politicians
—i. End talk about economy
–j. End talk about tomatoes
-k. End talk about Gertrude
l. End talk about supermarket
Since we are ending stories _in reverse order_ from the order we started them, this is a “Stack” (Last In First Out structure).
Now imagine once you reach the story about the grandfather, you, like my grandmother, lose track of why the FUCK you’re saying any of this, so you’re just like “Your grandfather — he uh — yeah, he was a good guy.”
ALL of the stories get wrecked. You aren’t finishing ANY of them now.
That’s basically the “Overflow” — too many individual stories added (the “stack” of stories is bigger than you can remember the order of, more stories than 1 person could keep track of, and so the entire thing derails).
This is why, if I start telling someone a story and I’m 2-3 stories deep and someone interrupts me and makes me lose my place, I tell them they “stack smashed” me.
A stack overflow can be used to inject custom code that normally wouldn’t be allowed. If a computer instruction is structured to for instance send 8 bits to be stored in the memory stack, then run 8 bits of code, so like MMMMMMMMCCCCCCCC, if you have a stack overflow exploit it may let you for example send 16 bits into the memory part of the command, so it will fill up all the 8 Ms with memory write bits, but then continue to overflow bits from your memory string into the CCCCCCCC section afterwords, which would then be executed as a live software instruction, rather than a more inconsequential memory write.
Have you ever tried to suspend one thought, to think of something else relevant first?
Imagine that you want to by groceries, so you need to drive to the supermarket, so you need to start your car, so you need to get your keys, so you need to search your pockets, and technically you are now 4 nested-tasks deep in your plan to buy groceries.
A computer sometimes has to do nested tasks like this too (but usually dealing with data, like to do a calculation it needs to know some some numbers, but that numbers requires some arithmetic, and that arithmetic requires looking up some data, etc etc).
When a computer is nesting these tasks, it stores its memory of what it was doing on a virtual ‘stack’, and keeps trying to solve the problem that is on top of the stack.
The computer has a limited amount of memory, so if the task is nested too many times, the ‘stack’ will overflow beyond the memory it uses to organise this ‘stack’. It doesn’t know what to do in that case, and so to be safe the program will crash and simply stop running.
The stack is the space where a computer stores the details of a program that it’s running. Not the data, but which arguments it was given, for example.
One way to make a large program easier to read or maintain is to split it up into small functions that do one thing and pass the result either back to the original program or on to another function.
Each time a function is called, some space is allocated in memory on the stack to reference where the function code is, what input values were passed to it, etc. When a function returns back to the one that called it, those entries are removed from the stack.
Sometimes a bug (or badly written program) will add too many entries for the memory in the computer to handle, and that’s when you get a stack overflow. This can happen due to a function calling a function, etc, without ever ‘returning’, or some error in the logic causing an infinite loop that will never return (usually in recursion).
Hence the stack will ‘overflow’ the amount of space given by the operating system.
Imagine you’re typing instructions onto pieces of paper, and then putting them into a cardboard box. Pretty soon you’ll have a **stack** of papers. Now imagine that you want to close the box, but you can’t because you put too many papers onto the stack; that’s an **overflow**.
In a computer, the box is called memory and it collects a stack of instructions called machine code. Eventually, if you keep putting instructions into the box faster than the computer can take them out and read them, the stack of instructions will get too big for the box. If that happens, your computer won’t be able to reach the top of the stack of instructions anymore, so it gives you an error to let you know it can’t do its job anymore. That’s called a stack overflow.
If you need a more technical answer, the stack is a fixed-size sequence of memory that is set aside for instructions during a software application’s runtime. It is called a stack because it uses the FILO (first in, last out) paradigm of command execution, wherein instructions are executed in reverse order of when they were placed, such that the last instruction stored in the stack will be the first instruction removed from it. Once the stack is full, it cannot accept new instructions before processing what it already has, so it will give a stack overflow error if you try to give it more instructions at once than it can handle.
[Stack Overflow](https://stackoverflow.com/) is also the name of a forum programmers often go to for questions about programming.
Consider that you are doing grocery shopping, and you have one of those amazon basic carts to carry your stuff home.
Now we modify the cart in such a way that you can only store items vertically, one on top of each other.This is known as a stack.
You can put items in it, but the first one you put will be last one to take out, because obviously it will be in bottom of the stack. This is called FILO or First in Last out.
Now, Imagine a situation, where there is not space in the cart and you are trying to put items in it. This is stack overflow.
In reality you can feel the stack because you can see it so you know yourself that it is full, half or otherwise empty.
In computer science, stack(the cart) is represented by memory, and the items are nothing but instructions that your program wants processor to perform. Because ultimately processor is going to perform.
Stack overflow is a disastrous error in computing that happens when your program is writing constantly and continuously to the memory, something like recursion – where a section of code is capable of calling itself.
Basically, you cannot feel the memory (stack) like in case of your cart, so you don’t know whether it is full, half or empty.
The stack is a data structure, but this refers to a very specific stack. In programming you have these things called “functions” which do something, that can be as simple as adding to numbers, or they can call other functions.
Every time a function is called, the memory (RAM in this case) it has requested upfront to do its work, that memory is allocated on a stack, meaning that the latest functions to be called will be removed first as they finish and return.
A stack overflow is when too many functions have been nested and the RAM reserved for the function stack runs out, or “overflows”.
Many other commenters are describing what I would call a “buffer overflow”, and not a proper “stack overflow”.
ELI5: A stack overflow is when the computer thinks the computer programmer is a dumbass and says “fuck this shit, I give up” because the programmer told it to perform a task which contains itself and the computer has been doing the same thing over and over again for too long.
ELI25: A computer has something called a “runtime stack”. The runtime stack is responsible for keeping track of program state when a “function” is called. This is necessary because computers are stupid and have a VERY short attention span. While a computer can have many gigabytes of RAM memory, the amount of memory that is actually directly accessible to the computer at any given moment in time is about 100 bytes worth of CPU registers or so. So every time a function is called, the computer has to write down the contents of the CPU registers (along with any input parameters) into a “stack frame” which is placed on top of the runtime stack. This is necessary so that when the function has completed, the computer program can take the stack frame off of the runtime stack, restore the CPU registers to their original state and pick up where it left off (usually with some additional data that the function has written to the stack frame for the program to use).
Important to our discussion is the fact that functions can call other functions. Each function that is called requires its own stack frame to be placed on top of the runtime stack, causing it to grow. Of particular note is the fact that functions *can call themselves*. This is called “recursion”, which is commonly used in a programming strategy called “divide and conquer”. Recursion can be a dangerous technique, because if you don’t write your function carefully, it will keep calling itself over and over again. This is called “infinite recursion”, and is the most common causes of stack overflows.
Modern operating systems specify a maximum size for a runtime stack. If the runtime stack tries to access unallocated memory, the computer hardware (on a modern computer) detects this automatically and causes a “page fault” to occur. A “page fault” will automatically halt the program’s execution and hand control back to the operating system, which has the power to decide the program’s fate. If the runtime stack has not exceeded the maximum size, the operating system will allocate more memory for it to use and allow it to occur. If the maximum size HAS been reached, then the operating system kills the process immediately, reports a “stack overflow error” and begins cleaning up all of the program’s resources.
On very old or simple computers, the above mentioned process for detecting a stack overflow does not happen, and the program will keep running while growing the runtime stack indefinitely. Eventually the runtime stack will grow so large, that it will start overwriting other data which will inevitably cause Bad Things(tm) to happen. If the system vectors are overwritten, the computer can forget how to perform basic tasks like multiplication or how to turn itself off. Worst case scenario, malicious code can take advantage of a stack overflow in order to corrupt other programs and gain total control over the system.
Stack Overflow is a programming forum.
On a serious note: A Stack is a data storing structure. You can put information in, and take information out. A stack works in a LIFO (last in first out) format, where the last item you put in, will be the first you take out, like a stack of stuff IRL (hence the name stack).
A stack overflow is when you try to add data to a stack that has reached the item limit. Like when you try to stuff a box which is already full.
Keep in mind that a stack is coded to have an infinite storage space, but our RAM can only handle so much right? That’s why we set a data limit to a stack. A stack overflow happens when we exceed the limit we placed. The program will prohibit us from adding anymore to the full stack.
Imagine a really long parking lot that continues north effectively forever.
Spaces 1-1000 a reserved for trucks that run the parking lot. Other than that people can reserve spots to store trucks in for whatever jobs they do, coming and going as they please. As a general rule trucks that come in last will be the first to leave, but fancy programmers don’t have to follow this.
One day somebody reserves spaces 1200 to 1210, AND THEN brings in 11 trucks. When there are no more spaces and they park number 11 in somebody else’s reservation, that is stack overflow.
If the parking lot has protections against this, somebody will run out screaming “Stop stop, somethings wrong. Take all the trucks out and make a new reservation! ”
If they don’t have protections against this then they’ll be free to park in 1211 and when the real 1211 (or another problematic reservation) shows up they’ll just push whatever is there out of the spot and be fine with that.
This in contrast to underflow, when somebody goes to get a truck and the reservation is empty.
Latest Answers