
Stack is one of the ways to implement this process efficiently. The memory blocks are freed when the function terminates.

When a function is called, memory is allocated to all local variables somewhere and you can access those variables as you know their locations. It is a special area in computer’s memory which is used to store local function variables. Stack segment is a memory management technique used for static memory allocation. Let’s discuss the two in detail and compare them to understand which one is better. Stack segment is used to store local function variables that are created automatically, whereas heap segment is used for dynamically allocated memory.īoth are stored in computer RAM and they can grow and shrink during the execution of a program.

Stack and Heap are the two ways memory is allocated in the operating system. The operating system allocates memory for each process which is divided into segments.

The goal is to prevent any process from accessing the memory that has not been allocated to it yet. Memory management is a fundamental phenomenon of the operating system used to handle or manage primary memory in order to control the memory access rights on the computer.
