0% found this document useful (0 votes)
377 views

Storage Allocation Strategies in Compiler Design

Uploaded by

mcsos0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
377 views

Storage Allocation Strategies in Compiler Design

Uploaded by

mcsos0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

Storage Allocation Strategies


There are mainly three types of Storage Allocation Strategies:

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;

Advantages of Static Allocation


1. It is easy to understand.

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.

Disadvantages of Static Allocation


1. Not highly scalable.

2. Static storage allocation is not very efficient.

3. The size of the data must be known at the 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];

Advantages of Heap Allocation


1. Heap allocation is useful when we have data whose size is not fixed and can change during the
run time.

2. We can retain the values of variables even if the activation records end.

3. Heap allocation is the most flexible allocation scheme.

Disadvantages of Heap Allocation


1. Heap allocation is slower as compared to stack allocation.

2. There is a chance of memory leaks.

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.

Frequently Asked Questions

Q.1: What is the purpose of using storage allocation strategies in compiler


design?

You might also like