Storage Allocation Strategies in Compiler Design (original) (raw)

Last Updated : 18 Apr, 2026

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.

Below is an example of a C code to demonstrate how a C compiler typically decided memory allocation.

memory_layout_of_c_program

There are mainly three types of Storage Allocation Strategies which compiler uses:

Static Storage Allocation

Memory for variables is allocated at compile time, and each variable is assigned a fixed memory location that remains constant throughout the entire execution of the program. The compiler determines the memory addresses before the program starts running, and no changes are made to these allocations during execution.

int number = 1; // Global variable static int digit = 1; // Static variable

Stack Storage Allocation

Used to manage memory for local variables and function calls. Memory is allocated automatically when a function is called and deallocated when the function returns. It follows the Last-In, First-Out (LIFO) principle, where the most recently added function call is removed first.

Each function call creates an activation record (stack frame) that stores local variables, parameters, and return information. When the function execution completes, its stack frame is removed, and the memory is released automatically.

// when we call the sum function below, memory // will be allocated for the variables a, b and ans

void sum(int a, int b) { int ans = a + b; cout << ans; }

**Heap Storage Allocation

Form of dynamic memory allocation in which memory is allocated at runtime during program execution. Unlike stack memory, heap memory is not automatically deallocated when a function ends, allowing data to persist beyond the function’s lifetime.

Memory in the heap is managed manually using allocation and deallocation functions. In C/C++, functions like malloc() or new are used to allocate memory, and free() or delete are used to release it.

int* ans = new int[5];

**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