ELI5: Explain Like I'm 5

Stack-based memory allocation

Imagine you have a big box called a memory, and you need to store some things inside it. Instead of just putting everything anywhere in the box randomly, we use a stack to organize them.

A stack is like a tower of blocks where you can only add new blocks to the very top. So when we get something new to store, we put it on top of everything else. Whenever we need to use something, we always take the thing from the top first.

Similarly, when a program needs to store some data, it uses a stack to organize the memory. The memory is divided into small parts called frames. Every time we need to store something new, we create a new frame on top of the previous one. As we keep adding more frames, the stack grows taller and taller.

When we are done with a frame, we throw it away and everything on top of it comes down. Just like taking a block from a tower of blocks makes everything on top of it fall down. So the stack shrinks back to its original size.

This way, the program can keep track of what data it needs to access next very quickly. We just always take the topmost frame on the stack. This method of organizing memory is called stack-based memory allocation, and it helps us use memory in a very efficient way.