Difference between Static Allocation and Heap Allocation
Static Allocation v/s Heap Allocation
Static Allocation: Static allocation is an allocation procedure that is used for the 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, the compiler decides the extent of storage which cannot be changed with time, hence it is easy for the compiler to know the addresses of these data objects in the activation records at a later stage. Static allocation is implemented in FORTRAN.
Heap Allocation: Heap allocation is an allocation procedure in which the heap is used to manage the allocation of memory. Heap helps in managing dynamic memory allocation. In heap allocation, the 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 reuses the deallocated space using the best fit.
The difference between Static Allocation and Heap Allocation is as follows:
||Static allocation allocates memory on the basis of the size of data objects.
||Heap allocation makes use of heap for managing the allocation of memory at run time.
||In static allocation, there is no possibility of the creation of dynamic data structures and objects.
||In heap allocation, dynamic data structures and objects are created.
||In static allocation, the names of the data objects are fixed with storage for addressing.
||Heap allocation allocates a contiguous block of memory to data objects.
||Static allocation is a simple, but not efficient memory management technique.
||Heap allocation does memory management in very efficient manner.
||The static allocation strategy is faster in accessing data as compared to heap allocation.
||While heap allocation is slow in accessing as there is a chance of creation of holes in reusing the free space.
||Static allocation is inexpensive, it is easy to implement.
||While heap allocation is comparatively expensive.
||Static memory allocation is preferred in an array.`
||Heap memory allocation is preferred in the linked list.
||Static memory need not to be de-allocated in every programming language
||Whereas heap memory in programming languages like C/C++, de-allocation of memory is necessary or else the program might have undefined behaviour
||Direct access to memory addresses
||Indirect access using pointers
||Typically used for global/static variables
||Used for dynamically allocated data
||Fast, as memory is pre-allocated
||Slower, as memory is allocated at runtime
||Not flexible, fixed lifetime
||Flexible, deallocation can be performed at any time
- p = malloc(sizeof(int));
- int arr = new int[SIZE];
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI
as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.