In order to know what a stack overflow is, you first need to know what a stack is. A stack in computer terms is where data is stored temporarily when a program is using it. There are many different types of stack, such as the “execution stack”, “control stack”, “machine stack” and “run-time stack”.
They all do different things, but mainly all you need to know is they temporarily store data until they are needed.
When computers work things out, they have to take information from a memory location, use it to make some kind of calculation, and then post the result of that calculation back into the memory. In order for it to do this, part of the information it needs is the address of the memory location to post the result. Every memory location has a unique address.
Think of it like sending a broken television back to the TV Company for repair. You send the TV, along with instructions telling them what’s broken. You also send them the address you want the TV sent back to. The parcel leaves your house, the TV Company repairs it, and then it gets sent back to the address you specified. This is a bit like what the computer does when working out calculations. It uses the return address information to know where to send the answer.
There is a special stack that is used called the “call stack”. This is where computers temporarily store memory address information while they are carrying out calculations. Programs use the call stack to load it with the addresses of memory locations. The call stack then stores them until the programs want them again. The program should then delete the address location from the call stack when it’s finished.
Sometimes, if a program is badly written or if you don’t have enough memory in your computer, the call stack can become full. This is what is known as a stack overflow, and if this happens your computer may crash. This can also happen if the memory in your computer is faulty.
The usual cause for this type of problem is when a program doesn’t delete addresses from the call stack properly. Normally it should store a memory address, calculate an answer, post the answer back, and then delete the address record from the call stack.
Sometimes however, a program may not delete the address record, and the call stack get’s full up, causing an overflow.
Another reason is if a program gets locked into a loop. Instead of calculating an answer and posting it back, it instead asks for another question. Sometimes this could go on forever (known as an infinite loop). When this happens, it doesn’t matter how much memory you have, sooner or later the call stack will fill up and your computer will crash.