Stack and heap

Stack is a simple data structure that follows LIFO. Automatic and local variables are stored in stack.




When inside the A(), the return address will be address of the instruction B(). Although it is nowhere mentioned, but a call instruction is called in the backend.

A stack frame is the current view into the stack relevant to the currently executing function; The set of values pushed for one function call is termed a “stack frame”;

Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.


Heap is used for dynamically allocated variables that need to exist outside the current stack frame.

A heap takes up a large amount of memory from OS, when initializing. It then gives small chunks of memory, as per the request by the application