Stack vs Heap

By | October 6, 2021
Stack vs Heap

When we compile our program, it requires memory to execute, in high-level languages Operating system and the translator take care of the memory management. The memory that would be assigned to our program depends on the operating system and its memory management architecture.

In most of the programming languages like C++, C, Java, etc the Operating system divide the program into 4 memory blocks which are Code, Static or Global, Stack and Heap.

Vamware
  • The Code block handles Instruction.
  • the static or global block deal with Global variables
  • Stack deal with functions and local variables
  • Heap deals with dynamic variables and functions.

Here in this article, we will only compare the two most important blocks of the memory management which are Stack vs Heap.

What is Stack Memory?

Stack memory is the contiguous block of memory which handle your program functions (including main() functions as well as user-defined functions) and local variables. It stores all the functions and variables temporary until their execution.

It basically works on the concept of Stack data structure which is LIFO (Last in First Out). It uses the push and pop operation to execute the function, the function pushed in the stack memory first executed at last because of the LIFO principle.

The main advantage of the stack memory is developer does not need to allocate and deallocate memory for the variables and function, stack handle all it for you. Once the function call is done the stack free the memory automatically by popping out the function and its variable from the memory block.

What is Heap Memory?

Heap memory is that part of the memory block which is control by the user rather than the operating system. It is also known as dynamic memory allocation. A user can manually control the Heap memory, in this memory region, the user has control over how much part of the memory he wants to allocate his program functions and variables.

In Stack memory, the CPU allocate and deallocate memory to the program functions and variables automatically but in Heap memory, the user has to do the allocation and deallocation process.

If the user allocates a hep memory to one of its variables and did not deallocate, it would be stored in the memory forever, which can cause many problems because each block of memory is important and no one wants any unwanted variable or function to occupy the program memory.

In C++ and C languages we use malloc(), calloc(), and new to allocate heap memory to a variable, to deallocate the memory we use free and delete statements.

Difference between Stack vs Heap: Head to Head Comparison

Stack Heap

Memory Structure

Stack has a linear memory structure. Heap follow the hierarchical data structure

Speed

Stack has high access speed Heap has slow access speed as compared to stack

Memory Management

  • CPU handle memory management with the help of the Operating system.
  • Memory allocation and deallocation handled automatically
  • In heap, User has to control the memory management.
  • Memory allocation and deallocation are done by the user itself.

Variable Scope

The variable scope is limited until the function execution The variable will remain in the memory until the deallocation statement.

Memory Size

The stack provides limited memory, it depends upon the operating system.

If the program requires more than the stack memory, the program will crash.

Heap does not have a specific limit on memory size.

 

Variable size

In stack, we cannot resize the variables In heap, we can resize the variables

Memory allocation

The memory allocated to the function in a contiguous format. Random block of heap provided to the variables, that’s why we use pointers with heap memory variables.
Memory Allocation & Deallocation
CPU allocate and deallocate the memory Use allocate and deallocate memory by himself

Flexible

The stack is not flexible because it provides a limited amount of memory. Heap is very flexible.

Advantages

  • CPU manages the memory allocation and deallocation.
  • Object get automatically deleted once the work is done
  • It is more memory efficient
  • Accessing and executing speed is very fast.
  • Follow the concept of Last in First Out (LIFO).
  • You have control over the memory system.
  • You can allocate and deallocate memory by yourself.
  • The object would not kill until you want.
  • Your program does not limit to the memory size and it reduces the chances of a program crash.
  • Randomly access any variable

Disadvantages

  • Memory is limited
  • With many objects and limited memory, the stack can overflow and it can crash the program.
  • Random access is not possible

 

  • Access time is very slow.
  • It is more complex to implement.

 

When to use Heap?

We use heap memory when we deal with a large number of objects and we require a large amount of memory to execute our program. We can also use heap memory when we want to functions and variables run until the program execute.

When to use Stack?

It normally used with a smaller number of objects and functions. It is very fast and limited so it provides efficiency to program with fewer objects or functions.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *