Heap overflow and Stack overflow

The computer’s RAM has a few sections in it. Two of them are [heap](https://www.prepbytes.com/blog/heap/heap-operations/ “heap”) and stack. Both heap and stack store variables.

Let’s see about Heap and Stack briefly:-

Both sections of RAM that are Stack and Heap are for storage but there are a few differences some of them are:-

  1. The size of stack memory is smaller than the size of heap memory
  2. Access to stack memory is faster than access to heap memory.
Stack Heap
Size of stack memory is smaller Size of memory Size of heap memory is larger
Stack memory is allocated in a Last-In-First-Out (LIFO) manner Order of memory allocation Heap memory is dynamically allocated and It does not follow any order.
Resizing variables is not allowed in a stack. Once allocated can’t resize Resizing Resizing of variables is allowed in a heap
Memory is Deallocated automatically Deallocation We have to free the memory occupied.

Heap overflow:-

Heap is a section of RAM that stores dynamic variables. The dynamically allocated variables in the heap don’t follow any order. These variables are allocated using malloc() and calloc() functions and resized using realloc() functions in C and using new keywords in C++. These are inbuilt functions and keywords. These variables can be accessed globally. It’s our responsibility to free that memory space after use that we have allocated. There are two situations that can result in heap overflow:

  1. If we continuously allocate memory for variables and we do not free that allocated memory space after use it may result in memory leakage. The reason for heap overflow is because the memory space is limited and the allocated memory is still in use but not available for other processes.

Code

// C program to show heap overflow by 
//continuously allocating memory
#include<stdio.h>
int main(){
    for (int i=0; i<1000000000; i++){
       // Allocating memory without 
      //freeing it will cause
     // memory crisis
    int *ptr = (int*)malloc(sizeof(int));
    }
}
  1. If we allocate a large number of dynamic variables.

Code

// C program to show heap overflow
// by allocating large number of dynamic 
//  variables

#include<stdio.h>
int main(){
    int *ptr = (int *)malloc(sizeof(int)*10000000));
}

Stack Overflow:

Stack is also a section of RAM that is used to store local variables. The local variables are temporary variables used inside the function, parameters passed through a function, and their return addresses. All the allocated space in the memory by the local variables is freed automatically after the complete execution of the function. The user does not have any need to free up stack space manually. Stack is a Last-In-First-Out data structure because the last called function is executed first.
In the RAM, the stack is limited in size. If a program uses more memory space than the stack size then stack overflow will occur, This stack overflow causes a program to crash. There are two cases in which stack overflow can occur:

  1. If we declare an array or matrix or any higher dimensional array of huge size.

Code

// C program to show stack overflow
// by allocating very large space for 2D matrix

#include<stdio.h>  
int main() {
   // Creating a 2D matrix of size =(10^9 * 10^9) 
  // results to stack overflow.
   int mat[1000000000][1000000000];
}
  1. If a function recursively calls itself, the recursive functions are pushed onto the stack. In such cases, the stack cannot store large numbers of local variables used by every function call, resulting in an overflow of the stack.

Code

// C program to show stack overflow
// due to a non-terminating recursive function.

#include<stdio.h>  
void call(int x){
    call(x);
}

int main(){
   int x = 2;
   call(x);
}

This article tried to discuss Heap overflow and Stack overflow. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

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