4 .
Explain static storage allocation strategy with example and dis
cuss its limitations?
Static storage allocation strategy
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.
Int p; stack
Int Q=10;
Main(){ Heap
Int localvar; BSS
Int *r; Data
R=(int*)malloc(40); code
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;
static int digit = 1;
Advantages of Static Storage Allocation
1. The allocation process is straightforward and 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 Storage Allocation
1. Not highly scalable and offers limited memory flexibility.
2. The size of the data must be known at the compile time.
3. 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
Void sum(int a, int b){int ans = a+b; cout<<ans;}
Advantages of Stack Storage Allocation
1. Stack allocation provides very fast memory access and
management.
2. Memory is automatically managed and deallocated when a function
completes.
3. Memory allocation and deallocation is simple (compared to heap) as
completely controlled by compiler.
Disadvantages of Stack Storage Allocation
1. Stack has a limited size which can lead to potential stack overflow
errors.
2. It cannot handle dynamic or large memory requirements effectively.
3. 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];
Advantages of Heap Storage 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 as
programmers can decide when to allocate and when to deallocate.
Disadvantages of Heap Storage Allocation
1. Heap allocation is slower as compared to stack allocation.
2. There is a chance of memory leaks (in languages where automatic
garbage collection does not happen) if programmer forgets to
deallocate.
3. It can create dangling pointers / references if not handled carefully.