Storage Allocation Strategies in Compiler Design
Storage Allocation Strategies in Compiler Design
Design
A compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level Language)
like machine language. In a compiler, there is a need for storage allocation strategies in Compiler
design because it is very important to use the right strategy for storage allocation as it can directly
affect the performance of the software.
1. Static Allocation
2. Heap Allocation
3. Stack Allocation
1. Static Allocation
Static allocation lays out or assigns the storage for all the data objects at the compile time. In static
allocation names are bound to storage. The address of these identifiers will be the same throughout.
The memory will be allocated in a static location once it is created at compile time. C and C++ use
static allocation.
For example:
int number = 1;
static int digit = 1;
2. The memory is allocated once only at compile time and remains the same throughout the
program completion.
3. Memory allocation is done before the program starts taking memory only on compile time.
2. Heap Allocation
Heap allocation is used where the Stack allocation lacks if we want to retain the values of the local
variable after the activation record ends, which we cannot do in stack allocation, here LIFO scheme
does not work for the allocation and de-allocation of the activation record. Heap is the most flexible
storage allocation strategy we can dynamically allocate and de-allocate local variables whenever
the user wants according to the user needs at run-time. The variables in heap allocation can be
changed according to the user’s requirement. C, C++, Python, and Java all of these support Heap
Allocation.
For example:
int* ans = new int[5];
2. We can retain the values of variables even if the activation records end.
3. Stack Allocation
Stack is commonly known as Dynamic allocation. Dynamic allocation means the allocation of
memory at run-time. Stack is a data structure that follows the LIFO principle so whenever there is
multiple activation record created it will be pushed or popped in the stack as activations begin and
ends. Local variables are bound to new storage each time whenever the activation record begins
because the storage is allocated at runtime every time a procedure or function call is made. When
the activation record gets popped out, the local variable values get erased because the storage
allocated for the activation record is removed. C and C++ both have support for Stack allocation.
For example:
void sum(int a, int b){int ans = a+b;cout<<ans;}
// when we call the sum function in the example above,
memory will be allotted for the variable ans
Conclusion
In conclusion, different storage allocation strategies play an important role in determining the best-
fit storage allocation strategy according to the need of the user as the helps in determining how the
memory is going to be allocated and deallocated. Different storage allocation strategies have their
own advantages and disadvantages and the choice depends on the factors like speed, memory
allocation, efficiency, etc. So we can choose the allocation strategy according to the requirement.