Static Memory Allocation
Memory allocated during compile time is called static memory.
The memory allocate is fixed and cannot be increased or decreased during run time.
Example:
int main(){
int arr[5] = {1, 2, 3, 4, 5};
}
memory is allocated at compile time and is fixed.
Problems Faced in Static Memory Allocation
- If you are allocating memory for an array during compile time then you have to fix the size at the time of declaration. Size is fixed and user cannot increase the size of the array at run time.
- If the values stored by the user in the array at run time less than the size specified then there will be wastage of memory.
- If the values stored by the user in the array at run time is more than the size specified then the program may cras or misbehave.
Dynamic Memory Allocation
The process of allocating memory at the time of execution is called dynamic memory allocation.
- malloc()
- calloc()
- realloc()
- free()
Heap is the segment of memory where dynamic memory allocation takes place.
Unlike stack where memory is allocated or deallocated in a defined order, heap is an area of memory where memory is allocated or dealloacted without any order or randomly.
There are certain built-in function that can help in allocating or deallocating some memory space at run time.
Note: Play an important role in dynamic memory allocation. Allocated memory can only be accessed through pointer.
