Heap overflow and Stack overflow Last Updated : 10 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. CPP // C program to demonstrate heap overflow // by continuously allocating memory #include<stdio.h> int main() { for (int i=0; i<10000000; i++) { // Allocating memory without freeing it int *ptr = (int *)malloc(sizeof(int)); } } Time Complexity: O(1)Auxiliary Space: O(1) If we dynamically allocate large number of variables. CPP // C program to demonstrate heap overflow // by allocating large memory #include<stdio.h> int main() { int *ptr = (int *)malloc(sizeof(int)*10000000)); } 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 // C program to demonstrate stack overflow // by allocating a large local memory #include<stdio.h> int main() { // Creating a matrix of size 10^5 x 10^5 // which may result in stack overflow. int mat[100000][100000]; } 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. CPP // C program to demonstrate stack overflow // by creating a non-terminating recursive // function. #include<stdio.h> void fun(int x) { if (x == 1) return; x = 6; fun(x); } int main() { int x = 5; fun(x); } Please refer Memory Layout of C Programs for details. Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article make_heap() in C++ STL S shivani.mittal Follow Improve Article Tags : C++ system-programming Practice Tags : CPP Similar Reads Stack vs Heap Memory Allocation In C, C++, and Java, memory can be allocated on either a stack or a heap. Stack allocation happens in the function call stack, where each function gets its own memory for variables. In C/C++, heap memory is controlled by programmer as there is no automatic garbage collection.Stack AllocationStack al 7 min read Buffer Overflow Attack with Example A buffer is a temporary area for data storage. When more data (than was originally allocated to be stored) gets placed by a program or system process, the extra data overflows. It causes some of that data to leak out into other buffers, which can corrupt or overwrite whatever data they were holding. 3 min read make_heap() in C++ STL make_heap() is used to transform a sequence into a heap. A heap is a data structure which points to highest( or lowest) element and making its access in O(1) time. Order of all the other elements depends upon particular implementation, but remains consistent throughout. This function is defined in t 3 min read Heap in C++ STL The heap data structure can be implemented in a range using STL which provides faster max or min item retrieval, and faster insertion and deletion on sorted data and also works as a sub-routine for heapsort.STL Functions for Heap Operationsmake_heap(): Converts given range to a heap.push_heap(): Arr 6 min read sort_heap function in C++ The sort_heap( ) is an STL algorithm which sorts a heap within the range specified by start and end. Sorts the elements in the heap range [start, end) into ascending order. The second form allows you to specify a comparison function that determines when one element is less than another. Defined in h 3 min read C++ Program to Implement Binary Heap A binary heap is a complete binary tree that satisfies the heap property. The heap property states that for a max-heap, every parent node has a value greater than or equal to its children, and for a min-heap, every parent node has a value less than or equal to its children. Binary heaps are commonly 8 min read Like