Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Difference between Stack and Heap

In this article, we will learn what is memory allocation, what is stack and heap memory, and how stack memory works with an example of a program in c language, when stack overflow (error) happens in stack memory, advantage and disadvantage of stack memory, how heap memory works with an example of a program in c language, advantage and disadvantage of heap memory and the difference between stack and heap.

What is Memory Allocation?

Computer programs require a certain amount of memory to be allocated for execution. Memory allocation can be dynamic or static. In dynamic allocation (Heap Memory), memory is allocated at the program run time. A fixed amount of memory is allocated at compile time with the static allocation (Stack Memory). This allocated memory is released once the program execution is completed.

What is Stack Memory?

Stack is a linear data structure that is generally used when we need to follow the Last in First out (LIFO) manner. It stores the variables, references to objects, and partial results. Memory allocated to stack lives until the function returns. This means that the value stored in stack memory is only available while the execution is still running, and it will be automatically erased once the task is completed. If there is no space for creating new objects, it throws a stack overflow error.

How Stack Memory Works with an Example of a Program in C Language

We will understand the stack memory concept using the below example.

In the below program, we can see that there are two main functions. These functions and local variables are stored in stack memory which we can see below image.


int Addition (int num1, int num2)

      return num1 + num2;


int main()

int a = 2;
int b = 4;
int c = Addition (a,b);
return 0;




While program executes, first function main are stored in stack memory then variable a, b, and c are stored in stack memory.

After that function addition and variables num1, and num2 are stored in stack memory. That means that the first function addition is executed and then the main function is executed. These functions follow the LIFO manner.

When does Stack Overflow (error) happen in Stack Memory?

Stack overflow happens in stack memory while the program goes in an infinite loop means that recursive manner. Because after some time stack memory is filled with a recursive function and at that time overflow occurs.

The below program is an example of Stack overflow.

void work(int x)
   if (x== 0)
    x *= 2;
int main() 
   int y = 5;

Now, we will understand how above program is executed in stack memory.

In the below image, we can see that the first function main and variable y are stored in stack memory.

In the below image, we can see that function work is stored in stack memory. But function work calls itself for execution.

In below image, again function work is stored in stack memory because it calls itself again and again.

Now, our function goes in an infinite loop. That is why in the below example we can see that our stack memory is filled fully and stack overflow occurs.

Advantages of Stack Memory

  • Automatically clean up the project
  • High-Speed access
  • Variable can not be resized
  • Allocation and deallocation of memory can be controlled

Disadvantages of Stack Memory

  • Limited size of memory
  • During the compilation of code, stack overflow can happen

What is Heap Memory?

Heap is a hierarchical data structure . It does not have any manner like LIFO because it is a dynamic memory allocation. It uses dynamic memory allocation. All global variables are stored in heap memory space by default.

The allocation of heap memory occurs during the execution of the programmer’s instructions.The term heap refers to a collection of memory that the programmer can allocate and deallocate. As a result, the heap has no relation to the heap data structure.When compared to stack, heap stores data in a hierarchical manner, resulting in slower access.

How Heap Memory Works with an Example of a Program in C Language

int main()

     int *x;
     x = (int *)malloc(sizeof(int));  
    *x = 200;


The image below shows how memory allocation is accomplished using the malloc() function.

This allocated memory will be accessed further using the pointer variable shown below:

Advantages of Heap Memory

  • Unlimited size of memory
  • Access variable globally

Disadvantages of Heap Memory

  • Slower execution of time
  • Memory management is more complicated

Stack vs Heap

Parameter Stack Memory Heap Memory
Ordering It follows Last In First Out (LIFO) manner. It does not have any manner because it is a dynamic memory allocation.
Use Shortage of memory. Memory fragmentation.
Type of data structure A stack is a linear data structure. Heap is a hierarchical data structure.
Resizing Required Variable can not be resized. Resizing variables is possible.
Flexibility It is not flexible. It is flexible.
Cost Less More
Implementation A stack can be implemented in three ways: as an array, in dynamic memory, or as a linked list. Arrays and trees can be used to implement a heap.
Memory Size Limited memory and dependent on OS Unlimited memory
Efficiency Fast Slow
Allocation / Deallocation It is done automatically by the compiler. It is done manually by a programmer.
Access Speed High-speed access Slower compared to stack
Generation Of Space The stack is automatically allocated when a thread is created by the operating system. At run time, the language first calls the operating system to create heap space for the application.


1. What types of data can be stored in heap memory?
Heap memory is a dynamic memory that is used to store arrays, global variables, and any class instances (objects) created at runtime in Java that is referred to by reference variables from Stack memory.

2. Is heap memory permanent?
It is a scheme for allocating temporary memory in which data members are only accessible if the method() that contains them is currently running. It automatically allocates and de-allocates memory as soon as the corresponding method completes its execution.

3. Is stack memory faster?
Stack memory is much smaller in size than Heap memory. Stack memory is much faster than heap memory because of the simplicity of memory allocation (LIFO).

4. Is stack memory permanent?
A stack is a type of memory area in a computer that stores temporary variables created by a function. Variables are declared, stored, and initialized on the stack during runtime. It is a type of temporary memory.

Leave a Reply

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