Heap overflow and Stack overflow
Heap Overflow:
Heap is a region of process’s memory which is used to store dynamic variables. These variables are allocated using malloc() and calloc() functions and resize using realloc() function, which are inbuilt functions of C. These variables can be accessed globally and once we allocate memory on heap it is our responsibility to free that memory space after use. There are two situations which can result in heap overflow:
- If we continuously allocate memory and we do not free that memory space after use it may result in memory leakage – memory is still being used but not available for other processes.
// C program to demonstrate heap overflow// by continuously allocating memory#include<stdio.h>intmain(){for(inti=0; i<10000000; i++){// Allocating memory without freeing itint*ptr = (int*)malloc(sizeof(int));}}chevron_rightfilter_none - If we dynamically allocate large number of variables.
// C program to demonstrate heap overflow// by allocating large memory#include<stdio.h>intmain(){int*ptr = (int*)malloc(sizeof(int)*10000000));}chevron_rightfilter_none
Stack Overflow:
Stack is a special region of our process’s memory which is used to store local variables used inside the function, parameters passed through a function and their return addresses. Whenever a new local variable is declared it is pushed onto the stack. All the variables associated with a function are deleted and memory they use is freed up, after the function finishes running. The user does not have any need to free up stack space manually. Stack is Last-In-First-Out data structure.
In our computer’s memory, stack size is limited. If a program uses more memory space than the stack size then stack overflow will occur and can result in a program crash. There are two cases in which stack overflow can occur:
- If we declare large number of local variables or declare an array or matrix or any higher dimensional array of large size can result in overflow of stack.
// C program to demonstrate stack overflow// by allocating a large local memory#include<stdio.h>intmain() {// Creating a matrix of size 10^5 x 10^5// which may result in stack overflow.intmat[100000][100000];}chevron_rightfilter_none - If function recursively call itself infinite times then the stack is unable to store large number of local variables used by every function call and will result in overflow of stack.
// C program to demonstrate stack overflow// by creating a non-terminating recursive// function.#include<stdio.h>voidfun(intx){if(x == 1)return;x = 6;fun(x);}intmain(){intx = 5;fun(x);}chevron_rightfilter_none
Please refer Memory Layout of C Programs for details.
Recommended Posts:
- Buffer Overflow Attack with Example
- Check for integer overflow on multiplication
- Stack vs Heap Memory Allocation
- stack empty() and stack size() in C++ STL
- Get the stack size and set the stack size of thread attribute in C
- How to implement Min Heap using STL?
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- Stack in C++ STL
- stack top() in C++ STL
- stack swap() in C++ STL
- stack push() and pop() in C++ STL
- stack emplace() in C++ STL
- Stack Unwinding in C++
- C Program to find direction of growth of stack
- Inter-process Communication using a shared stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

