Skip to content
Related Articles
Heap overflow and Stack overflow
• Difficulty Level : Basic
• Last Updated : 25 Feb, 2018

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:

1. 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`` ` `int` `main()``{``    ``for` `(``int` `i=0; i<10000000; i++)``    ``{``       ``// Allocating memory without freeing it``       ``int` `*ptr = (``int` `*)``malloc``(``sizeof``(``int``));``    ``}``}`
2. If we dynamically allocate large number of variables.
 `// C program to demonstrate heap overflow``// by allocating large memory``#include`` ` `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:

1. 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`` ` `int` `main() {`` ` `   ``// Creating a matrix of size 10^5 x 10^5``   ``// which may result in stack overflow.``   ``int` `mat;``}`
2. 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`` ` `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.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.8392789,"math_prob":0.92470306,"size":2613,"snap":"2021-21-2021-25","text_gpt3_token_len":585,"char_repetition_ratio":0.1418168,"word_repetition_ratio":0.11804009,"special_character_ratio":0.24760811,"punctuation_ratio":0.08227848,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9529172,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-23T14:45:41Z\",\"WARC-Record-ID\":\"<urn:uuid:6f93e6bc-04a4-45b0-8185-2c3591890703>\",\"Content-Length\":\"107629\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d103ccc3-e7f2-4ef0-9daf-58335f82234a>\",\"WARC-Concurrent-To\":\"<urn:uuid:ece7b795-b6e8-4efa-a984-573a202ae040>\",\"WARC-IP-Address\":\"23.62.230.31\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/heap-overflow-stack-overflow/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:EN2OJ7PKCMIFGVL6HTKNW3P7HCQFOPOV\",\"WARC-Block-Digest\":\"sha1:GDE45BOX4D67IRPPCUHGZJVDEFMVKWID\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488539480.67_warc_CC-MAIN-20210623134306-20210623164306-00020.warc.gz\"}"}