
The C programming technique of allocating necessary memory to hold data during runtime is known as dynamic memory allocation. We’re sure you’re familiar with arrays. When you want to process data and you know how big it is, an array is the ideal option.
Assume you’re reading data from a file or accepting keyboard input. You have no idea how large the data may be in such a case. In such a situation, an array is not useful.
C has the capability of dynamic memory allocation to deal with such situations. At runtime, you can allocate Memory for your storage. When you’ve finished your task, you can deallocate the memory.
In the c programming language, we handle dynamic memory allocation by following four library functions, and the dynamic memory allocation in the c header file is stdlib.h.
- malloc()
- realloc()
- calloc()
- free()
Table of Contents
Difference between Static and Dynamic Memory Allocation
Following are the differences between Static and Dynamic memory allocation.
Static Vs Dynamic Memory Allocation
Static Memory Allocation | Dynamic Memory Allocation |
---|---|
Static memory allocation occurs when memory allocation is done at the time of compilation. | Dynamic memory allocation occurs when memory allocation is done at the run time. |
Static memory allocation is faster because it saves running time. | Dynamic memory allocation is slower than static memory allocation. |
We can not reuse the memory in case of Static Memory Allocation. | In case of Dynamic memory allocation we can reuse the memory. |
Memory allocation that is static is less efficient than memory allocation that is dynamic. | When compared to static memory allocation, dynamic memory allocation is more efficient. |
To manage static allocation of memory, it is use stack. | To manage dynamic allocation of memory, it is use heap. |
Also Read: Stack Vs Heap Memory
Key Points to Remember About Dynamic Memory Allocation in C
- You can use malloc() method for allocating memory in terms of bytes.
- When you use the malloc() method to allocate memory, it returns a reference (pointer) to the storage where the memory was allocated.
- When all of your memory-related tasks are completed, use the free() function to deallocate memory.
Syntax of malloc() function in C
We specified the kind of data to be stored and the number of bytes required to store a single data in memory in the malloc() parameter.
base = (dataType*) malloc(size); base = (int*) malloc(50 * sizeof(int));
Example of malloc() function in C
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int main() {
int i;
int * base;
int total = 0;
base = (int * ) malloc(MAX * sizeof(int));
if (base == NULL) {
printf("Unable to allocate memory");
exit(0);
}
printf("Enter elements: ");
for (i = 0; i < MAX; ++i) {
scanf("%d", base + i);
total += * (base + i);
}
printf("Total=%d", total);
free(base);
return 0;
}
Output: Enter elements: 10 20 30 Total=60
Syntax of calloc() function in C
base = (dataType*) calloc(n, size); base = (int*) calloc(10, sizeof(int));
Example of calloc() function in C
#include<stdio.h>
#include<stdlib.h>
#define MAX 4
int main() {
int i;
int * base;
int total = 0;
base = (int * ) calloc(MAX, sizeof(int));
if (base == NULL) {
printf("Unable to allocate memory for data");
exit(0);
}
printf("Enter elements: ");
for (i = 0; i < MAX; ++i) {
scanf("%d", base + i);
total += * (base + i);
}
printf("Total=%d", total);
free(base);
return 0;
}
Output: Enter elements: 11 22 33 44 Total=110
Syntax of realloc() function in C
base = realloc(base, <new-size>);
Syntax of free() function in C
We use free() function to deallocate memory in C. You can use malloc() or calloc() function to allocate memory. The function itself does not free the memory. You have to call free() method explicitly to free the memory.
free(base);
Difference Between malloc() and calloc() with Examples
malloc() | calloc() |
---|---|
The malloc() function creates a single block of the specified size provided by the user. | The calloc() function creates multiple blocks of the specified size. |
The malloc() function initializes the reserved memory block to a unexpected/garbage value. | The calloc() function initializes the reserved memory block to zero. |