A compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level Language) like machine language. It is also responsible for organizing the memory layout of a program by allocating space for global and local variables, constants, and dynamic data structures.
- As the program begins execution, it is under the control of the operating system, which sets up the environment by allocating space for the whole process.
- Compiler is responsible for deciding which variable gets which part of the memory.
- For example, the compiler needs to decide whether variables are best placed in the stack (for local variables) or in the heap (for dynamically allocated memory) or data segment (global and stack variables)
Below is an example of a C code to demonstrate how a C compiler typically decided memory allocation.

There are mainly three types of Storage Allocation Strategies which compiler uses:
Static Storage Allocation
Static storage allocation is the simplest form of memory allocation. In this strategy, the memory for variables is allocated at compile time. The compiler determines the memory addresses for all variables before the program starts running, and these addresses remain the same throughout the program's execution.
For example, in languages like C or C++, when you declare global variables or static variables, the memory for these variables is allocated using static storage.
int number = 1; // Global
static int digit = 1;
- The allocation process is straightforward and easy to understand.
- The memory is allocated once only at compile time and remains the same throughout the program completion.
- Memory allocation is done before the program starts taking memory only on compile time.
- Not highly scalable and offers limited memory flexibility.
- The size of the data must be known at the compile time.
- Static storage allocation is not very efficient and there is potential for memory wastage.
Stack Storage Allocation
Stack storage allocation is used for memory allocation of local variables of a function (including main), where memory is allocated at when the function is called. In this case, memory is assigned in a Last In, First Out (LIFO) manner, meaning that memory is allocated when a function is called and deallocated when the function returns.
In C/C++, the local variables of a function are stored in the stack. Each time a function is called, a new activation record (stack frame) is pushed onto the stack to store the local variables. Once the function completes, the stack frame is popped off, and the memory used is released.
// when we call the sum function below, memory
// will be allotted for the variables, a, b and ans
void sum(int a, int b){int ans = a+b; cout<<ans;}
- Stack allocation provides very fast memory access and management.
- Memory is automatically managed and deallocated when a function completes.
- Memory allocation and deallocation is simple (compared to heap) as completely controlled by compiler.
- Stack has a limited size which can lead to potential stack overflow errors.
- It cannot handle dynamic or large memory requirements effectively.
- The fixed memory size restricts flexibility as programmers have no control about allocation and deallocation of memory.
Heap Storage Allocation
Heap storage allocation is another form of dynamic memory allocation, but unlike the stack, it allows memory to be allocated at runtime for variables that can grow or shrink during execution. The heap is used for memory that is not automatically deallocated when a function call ends. This type of memory allocation is used when you need to store objects or data that are not tied to a specific function scope.
In C/C++, functions like malloc() or new are used to allocate memory in the heap, and functions like free() or delete are used to deallocate the memory.
int* ans = new int[5];
- Heap allocation is useful when we have data whose size is not fixed and can change during the run time.
- We can retain the values of variables even if the activation records end.
- Heap allocation is the most flexible allocation scheme as programmers can decide when to allocate and when to deallocate.
- Heap allocation is slower as compared to stack allocation.
- There is a chance of memory leaks (in languages where automatic garbage collection does not happen) if programmer forgets to deallocate.
- It can create dangling pointers / references if not handled carefully.
Comparison of Storage Allocation Strategies
Strategy | Memory Allocation Time | Memory Management | Efficiency | Flexibility | Usage |
|---|---|---|---|---|---|
Static Allocation | Compile time | Fixed, no deallocation | High | Low | Global and static variables |
Stack Allocation | Runtime | Automatic (LIFO) | Fast | Limited | Local variables, function calls |
Heap Allocation | Runtime | Manual (allocation and deallocation) | Slower | High | Large or dynamic data structures |

