I’d like to add that the most common place you’d see this is when you’d mess up a recursive function or have some circular function. Stacks are last in first out data structures and it’s the structure of choice for storing the currently running function.
The Stack is typically the memory used to run your function. If your function calls another function then you push the memory from the called function onto “the stack”. When you return from a function it discards the current stack and loads whatever is at the top of the stack.
So for example in a web app, you’d have a stack that would look like a routing function, calling a function on a controller class, calling a function on a service, that calls a function on a repository, that calls a function on a ORM, that calls a function on a database connection object, that calls a function that sends SQL off to a server. Each function has a place on the stack where it’s state is remembered for when the currently executing function returns.
If you use VSCode and set a debugging breakpoint you can inspect the “Call Stack”, it should be the second to the bottom tab in the debug window. Clicking on the stack should alter which locals you see in the “Variables” tab.
A stack overflow most commonly happens when you run out of memory and can’t put anything else on the call stack.
There were several programming languages explicitly based on a stack. The simplest one was used by HP calculators. These calculators could do fairly complex math, but it took practice. To add 1 and 2 together, you would type 2, hit enter, which would put that value on the stack. Next, 1 then enter, and tnat goes on top of the stack. Finally, you hit the plus key, which will then pull the top number off the stack (1) then add the second number off the stack (2), and put the answer (3) back on the stack. These calculators had crazy complicated functions (like matrix math) that could have you placing 20 numbers on the stack (in the right order of course).
The Forth language used a stack in a similar way, but as a general programming language. You could define functions and thet would grab values off the stack. So CIRCLE might grab 3 values, x y, and radius, to draw a circle on the screen. It was confusing as hell to write code, but it ran quickly. A derivative of forth, Postscript, is still used in laser printers. Look at a Hello World for postscript and you’ll see the stack used in all its glory.
That the program is out of its small, temporary space to write down things it needs to remember for a while. When you need to write things down not to get lost and you can’t – you get lost. You can’t complete the task. It usually means the program is broken. That it goes in circles writing the same things over and over again. Very rarely it could also mean the temporary space (the stack) is too small to fit the things. Now: why stack, and why it’s small and for only temporary things? Because it is a portion of a computer memory it gets for free. It is also the block of memory the same as the one the program is in. So – they are close to each other. The program and the stack. It’s near. It’s handy. The program can get this data very, very fast. It’s called stack because it is a stack. Like a stack of little papers. You can put a card on top, then take a card off the top. It’s also a fast thing. You don’t need to search. You just take a card. Now you know why it’s small and used for temporary things. When you need to store large data, you use a book. With page numbers, index and all. That is memory. In programming it’s often called heap. In order to get something from there you must specify what do you need and from which exact location. When you want something from stack you just take it. It’s the last thing you put there. When you solve problems you usually put some info for later, then take it from stack, when you take it and use it, it’s no longer taking space. So you put some data, take some data. And it never overflows. When you only put data, but you don’t take it out, it stays there, put too many little cards on the stack and it overflows. Putting data on stack without taking it from it doesn’t make sense. So if the program does it – it’s something wrong with it and it does something different from it was intended to do.
Just to add to the other answer:
All memory in a computer has to be declared and reserved in some way by a program, so your operating system knows that *this* block of memory currently belongs to your program and is not available to some other process running in the background. Trying to access memory that hasn’t been reserved results in the OS terminating the program.
In computers, memory can be reserved either on the stack or the heap. The heap is for more permanent memory reservation: you have to explicitly tell the computer how much memory you want to reserve, and then you have to tell it when you don’t want it any more and it can be freed for another program to use (side note: a “memory leak” occurs when a program reserves some memory, finishes using it, but due to a programming oversight never tells the OS to free it again). Once reserved, heap memory remains reserved until the program asks for it to be freed again.
Stack memory is slightly different: it is reserved by the OS automatically when a function (a particular section of your code) starts executing, and is automatically freed when the function finishes. It acts as a “scratch pad” for that function so you can do convenient things like this:
`int MyAddFunction(int a, int b)
{
int result = a + b;
return result;
}`
In this case: where is “result” stored? You never asked for that memory to be reserved. The answer is the stack: the OS will automatically reserve memory for “result”. Once the function finishes (at the return point), then the memory block for “result” is automatically freed up.
A stack overflow occurs when you use too much memory within a function. Stack size is pretty small (around 1mb I think?), so you could create a stack overflow either by trying to automatically reserve some huge data structure, or by running a loop that requests millions of little ones. In that situation, you would want to reserve some memory on the heap, and then remember to free it at the end of your function before it finishes.
Just to note: a lot of high level programming languages nowadays like Java, C# etc. manage memory for you so you don’t have to explicitly reserve it and free it on the heap.
Basically a place where programmers who think they’re better than everyone go to hate on any beginner trying to learn new concepts.
Oh, you mean like an actual stack overflow? A stack is basically a basket used by a program. When you use functions, variables, or store anything that is used short-term, it gets put on the stack. However, the stack/basket can only hold so much stuff.
(Some higher level stuff, but oh well) Let’s say you are using recursion to find the nth fibonacci number. If you want to find the 1,000,000th number, that means at least 1,000,000 things are getting put on the stack/basket. The stack is usually small because people don’t expect you to use tons of variables or function calls, so if you have a million things in your stack/basket you get a stack overflow error.
First, what is stack and why do you need it? It’s not just any random data structure, on most architectures you have push pop instructions and stack pointer register. What push does is increment sp and write operand to address sp points to, pop reads from address sp points to and decrements sp. Super useful for handling function parameters and return values. But what happens when someone writes an infinite recursive loop? Then stack grows without bound until it runs out of allocated memory, that is stack overflow exception.
imagine you have glasses in a pyramid like those fancy weddings. If you puir your drink in the top glass it will when full spill over into the next glass etc. the same happens in the computer memory, you fill it with so much data it spills over to the memory next to it. Now you can change data in other variables by spilling over in a clever way
Latest Answers