Difference between Static and Dynamic Memory Allocation in C
In C++, memory allocation is a process by which computer programs and services are assigned 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:
- Compile-time or Static Memory Allocation
- Run-time or Dynamic Memory Allocation
Static and dynamic memory allocation in C have different use cases and implications:
Static Memory Allocation | Dynamic Memory Allocation |
---|---|
In the static memory allocation, variables get allocated permanently, till the program executes or function call finishes. | In the Dynamic memory allocation, the memory is controlled by the programmer. It gets allocated whenever a malloc() is executed gets deallocated wherever the free() is executed. |
Static Memory Allocation is done before program execution. | Dynamic Memory Allocation is done during program execution. |
It uses stack for managing the static allocation of memory | It uses heap (not heap data structure) of memory for managing the dynamic allocation of memory |
It is less efficient | It is more efficient |
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 |
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. |
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. |
In this memory allocation scheme, execution is faster than dynamic memory allocation. | In this memory allocation scheme, execution is slower than static memory allocation. |
In this memory is allocated at compile time. | In this memory is allocated at run time. |
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. |
Let's take a look at each of them.
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.
Example:
#include <stdio.h>
int main() {
// Static memory allocation
// of an array
int arr[5] = {1, 2, 3, 4, 5};
// Printing the array elements
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
Output
arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5
In the above program, the array arr of size 5 is statically allocated, and it holds 5 integers. The memory for this array is fixed and is allocated when the program starts, based on the declared size.
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.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Dynamically allocate memory
// for an array of 5 integers
int *arr = (int*)malloc(5 * sizeof(int));
// Initialize the array with values
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
// Print the array elements
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
Output
arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5
This program dynamically allocates memory for an array of 5 integers using malloc, initializes the array with values 1 to 5, and prints the elements. It demonstrates basic dynamic memory allocation in C.