Programming Basics

Stack and Heap Memories

Programming Concepts

Senthil Nayagan
Senthil Nayagan Publication

--

Photo by Sergei Starostin from Pexels.

Overview of stack and heap memories

Both the stack and the heap are types of memory that are available to our code to use at runtime. Stack and heap memories are structured in different ways. All data stored on the stack must have a known and fixed size at compile time. On the other hand, data of an unknown size at compile time or a size that might change during runtime must be stored on the heap instead.

Stack memory

Stack of Pancakes | Photo Credit: Brigitte Tohm.

A stack is a special region of our computer’s memory where data is added or removed in a “last in, first out” (LIFO) manner. Think of a stack of pancakes as shown in the above picture — when we add more pancakes, we put them on top of the pile, and when we need a pancake, we take one off the top. Adding or removing pancakes from the middle or bottom wouldn’t work.

Adding data to the stack is called pushing, and removing data from the stack is called popping.

When a function executes, it may add some of its state data to the top of the stack; when the function exits, the memory will be automatically deallocated from the stack. The fact that when a function exits, all of its variables are popped off (deallocated) of the stack (and thus lost forever) is critical to understanding the stack. Thus, stack variables are local in nature.

Note that the size of memory to be allocated is known at compile time, and during run-time, the required memory gets allocated on the stack. Also, note that there is a limit (which varies with different OS) on the size of variables that can be stored on the stack — this is not the case for variables allocated to the heap.

Advantages of using stack

  • As the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and generally faster than heap-based memory allocation because the operating system never has to search for a place to store new data because that location is always at the top of the stack.
  • There is no need to manage the memory ourself — memory allocation and deallocation happen automatically.
  • As the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.

Disadvantages of using stack

  • Stack memory is very limited.
  • Random access is not possible.

Heap memory

Photo by PhotoMIX Company from Pexels.

As mentioned above, heap memory is one type of memory. Data with an unknown size at compile time or a size that might change (grow or shrink) during runtime must be stored on the heap.

How it works?

When we put data on the heap, we request a certain amount of space. The operating system finds an empty spot in the heap that is big enough, marks it as being in use, and returns a pointer, which is the address of that location. This process is called allocating on the heap and is sometimes abbreviated as just allocating.

Disadvantages of using heap

  • Allocating space on the heap requires more work because the operating system must first find a big enough space to hold the data and then perform bookkeeping to prepare for the next allocation.
  • Accessing data in the heap is slower than accessing data on the stack because we have to follow a pointer to get there.

--

--

Senthil Nayagan
Senthil Nayagan Publication

I am a Data Engineer by profession, a Rustacean by interest, and an avid Content Creator.