• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Storage Allocation Strategies in Compiler Design

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 decides how different variables will be organized in memory (such as stack, heap, or data segment), while the operating system allocates memory for the process during execution.
  • 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.

memory_layout_of_c_program
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 the memory addresses remain fixed throughout the entire execution of the program. The compiler determines the location of variables before the program starts running. This method is simple and efficient because memory is allocated only once and does not change during execution. However, it requires that the size of all variables be known at compile time. It also provides limited flexibility, since memory cannot be adjusted during program execution, which may sometimes lead to inefficient memory utilization.

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

Stack Storage Allocation

Stack storage allocation is used to manage memory for local variables of functions (including main). Memory is allocated when a function is called and automatically deallocated when the function returns. The stack follows the Last-In, First-Out (LIFO) principle, meaning the most recently called function is removed first.

main

In C and C++, each function call creates an activation record (stack frame) on the stack. This record stores the function’s local variables, parameters, and return information. When the function execution finishes, the corresponding stack frame is removed, releasing the allocated memory.

Stack allocation is fast and efficient because memory management is handled automatically by the system. However, the stack has limited size, which can lead to stack overflow if there are too many nested function calls or very large local variables.

// 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;}

Heap Storage Allocation

Form of dynamic memory allocation in which memory is allocated during program execution. Unlike stack memory, heap memory is not automatically released when a function finishes, allowing data to exist beyond the lifetime of a function call. It is typically used for data whose size may change at runtime or for objects that need to persist for a longer duration. Also provides high flexibility, as programmers can control when memory is allocated and released. However, it is generally slower than stack allocation and requires careful management. Improper handling may lead to memory leaks or dangling pointers if allocated memory is not correctly deallocated.

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];

Comparison of Storage Allocation Strategies

StrategyMemory Allocation TimeMemory ManagementEfficiencyFlexibilityUsage
Static AllocationCompile timeFixed, no deallocationHighLowGlobal and static variables
Stack AllocationRuntimeAutomatic (LIFO)FastLimitedLocal variables, function calls
Heap AllocationRuntimeManual (allocation and deallocation)SlowerHighLarge or dynamic data structures

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