Difference between Static allocation and Heap allocation

Static allocation:

In static allocation, space allocation for all the data objects is done at the compile time. In this type of allocation, all the decisions are made by the compiler for the extent of storage which will remain the same with time, as this will make it easy for the compiler to store the addresses of all the data objects in the activation records for other stages. In FORTRAN, static allocation is implemented.

Heap allocation:

It is also an allocation procedure in which the heap is used in the process of memory allocation. Heap is used for managing dynamic memory allocation. In heap allocation, it is possible to find the value of data objects even after the activation record. It basically maintains the linked list for the free blocks of spaces and reuses the deallocated space using the best fit.

Difference between static and dynamic allocation:

Static Allocation Heap Allocation
In this type of allocation, memory is allocated on the basis of the size of data objects. In this type of allocation, a heap is maintained for the memory allocation at the run time.
Static allocation is a simple way but not an efficient way of memory allocation. Heap allocation is an efficient way of memory allocation.
Static allocation is a faster way of accessing the data. Heap allocation is a slow way of accessing the data.
In an array, static allocation is used for allocating the memory. In the linked list, heap allocation is used for allocating the memory.
In this type of allocation, there is no chance of creating dynamic data structures and objects. In this type of allocation, dynamic data structures and objects are created.
In this type of allocation, the names of the data object can’t be changed with the storage for addressing. This type of allocation allocates a contiguous block of memory to the data objects.
This type of allocation is comparatively cheaper and easy to implement. This type of allocation is comparatively expensive and difficult to implement.
For example: int i; int a[10]; For example: s = malloc(sizeof(int));

This article tried to discuss the Difference between Static allocation and Heap allocation. Hope this blog helps you understand the concept. To practice problems based on heap feel free to check MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

Your email address will not be published. Required fields are marked *