Memory Allocation: Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations:
Static Memory Allocation: Static Memory is allocated for declared variables by the compiler. The address can be found using the address of operator and can be assigned to a pointer. The memory is allocated during compile time.
Dynamic Memory Allocation: Memory allocation done at the time of execution(run time) is known as dynamic memory allocation. Functions calloc() and malloc() support allocating dynamic memory. In the Dynamic allocation of memory space is allocated by using these functions when the value is returned by functions and assigned to pointer variables.
Tabular Difference Between Static and Dynamic Memory Allocation in C:
Static Memory Allocation
Dynamic Memory Allocation
1 In the static memory allocation, variables get allocated permanently. In the Dynamic memory allocation, variables get allocated only if your program unit gets active. 2 Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution. 3 It uses stack for managing the static allocation of memory It uses heap for managing the dynamic allocation of memory 4 It is less efficient It is more efficient 5 In Static Memory Allocation, there is no memory re-usability In Dynamic Memory Allocation, there is memory re-usability and memory can be freed when not required 6 In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed. 7 In this memory allocation scheme, we cannot reuse the unused memory. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when the user needs it. 8 In this memory allocation scheme, execution is faster than dynamic memory allocation. In this memory allocation scheme, execution is slower than static memory allocation. 9 In this memory is allocated at compile time. In this memory is allocated at run time. 10 In this allocated memory remains from start to end of the program. In this allocated memory can be released at any time during the program. 11 Example: This static memory allocation is generally used for array. Example: This dynamic memory allocation is generally used for linked list.