Stack Definition
Learn about the purpose and usage of the stack.
We'll cover the following
Introduction
The executable generated from the source code contains several static and dynamic sections. One of those sections is called the stack. The stack is used to store local variables.
Stack data structure recap
You may be familiar with the term stack from a data structures course. But, in case you aren’t, here is a quick explanation:
Stack is a LIFO (last in, first out) data structure. The last element we add to the stack is the first one to be removed.
View it as a stack of books on a desk, where:
- We only have access to the top book. To access the lower books, we have to remove the upper ones one by one.
- We can only add books on top of the stack. We can’t interact with the bottom book, or the entire thing will fall.
- We use an operation called
push
to add an element to the top of the stack.- We use an operation called
pop
to remove an element from the top of the stack.
In the following image, we can see this concept visualized:
Get hands-on with 1400+ tech skills courses.