Static allocation allocates memory based on the size of data objects.
int a = 5;
Memory is determined at the compile time, where the size and position of each data structure for example arrays, variable are constant throughout the program.
While this approach may help to give faster access, it does not offer as much versatility as the Heap Allocation.
Typically used for global/static variables.
The memory is available throughout the life cycle of the program from the time it starts until it terminates.
Used for dynamically allocated data.
p = malloc(sizeof(int));
Heap allocation
makes use of heap
memory for managing the allocation of memory at run time
(at the time of execution of the
program).
Heap
makes it possible to create dynamic data structures including linked lists
and trees
.
This flexibility is bought at the expense of extra cost in memory management, and the possibility of memory fragmentation.
Heap memory becomes freed after a certain duration of time or when the programmer determines that it should be freed, therefore it possesses a variable lifetime.
Heap allocation
does memory management in very efficient manner.
The static allocation strategy is faster in accessing data as compared to heap allocation.
Heap allocation is slower, as memory is allocated at runtime.
Static allocation is inexpensive, it is easy to implement.
Static memory allocation is preferred in array.