Static Allocation
Static Allocation
LIFETIME of a binding
– Static
– Stack
Heap
Static allocation
Static allocation is an allocation procedure which is used
for allocation of all the data objects at compile time. In this
type of allocation, allocation of data objects is done at
compile time only. As in static allocation compiler decides
the extent of storage which cannot be change with time,
hence it is easy for the compiler to know the addresses of
these data objects in the activation records at later stage.
Static allocation is implemented in FORTRAN.
Heap Allocation :
Heap allocation is an allocation procedure in which heap
is used to manage the allocation of memory. Heap helps
in managing the dynamic memory allocation. In heap
allocation, creation of dynamic data objects and data
structures is also possible as same as stack allocation.
Heap allocation overcomes the limitation of stack
allocation. It is possible to retain the value of variables
even after the activation record in heap allocation strategy
which is not possible in stack allocation. It maintains a
linked list for the free blocks and reuse the deallocated
space using best fit.
Static
Allocation Heap Allocation
Static allocation is
inexpensive, it is easy to While heap allocation is
6. implement. comparatively expensive.
Key Differences Between Stack and Heap Allocations
1. In a stack, the allocation and deallocation is automatically done by
whereas, in heap, it needs to be done by the programmer manually.
2. Handling of Heap frame is costlier than handling of stack frame.
3. Memory shortage problem is more likely to happen in stack whereas the
main issue in heap memory is fragmentation.
4. Stack frame access is easier than the heap frame as the stack have small
region of memory and is cache friendly, but in case of heap frames which
are dispersed throughout the memory so it cause more cache misses.
5. Stack is not flexible, the memory size allotted cannot be changed
whereas a heap is flexible, and the allotted memory can be altered.
6. Accessing time of heap takes is more than a stack.