Index
Dynamic memory allocation in C allows you to allocate memory at runtime using pointers. This is useful when the required amount of memory is not known in advance, as with arrays that may change in size. The C library provides four main functions for dynamic memory management:
malloc
(Memory Allocation)calloc
(Contiguous Allocation)realloc
(Resize Allocation)free
(Free Allocated Memory)
1. malloc
(Memory Allocation)
The malloc
function allocates a specified amount of memory in bytes and returns a pointer to the first byte of the allocated memory. If the allocation fails, it returns NULL
.
Example of malloc
:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
// Allocating memory for n integers
ptr = (int*)malloc(n * sizeof(int));
// Check if memory has been allocated successfully
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Assign values and display them
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Free allocated memory
free(ptr);
return 0;
}
Explanation:
malloc
allocates memory for 5 integers.- If the allocation is successful, we assign values to the allocated memory.
- Finally,
free(ptr)
deallocates the memory to avoid memory leaks.
2. calloc
(Contiguous Allocation)
The calloc
function allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer to the allocated memory.
Example of calloc
:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
// Allocating memory for n integers using calloc
ptr = (int*)calloc(n, sizeof(int));
// Check if memory has been allocated successfully
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Display values (they will be zero-initialized)
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
// Free allocated memory
free(ptr);
return 0;
}
Explanation:
calloc
allocates memory for 5 integers and initializes all of them to 0.- This is useful when you need zero-initialized memory.
3. realloc
(Resize Allocation)
The realloc
function adjusts the size of previously allocated memory to a new size, either expanding or shrinking it.
Example of realloc
:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
// Initial allocation for 5 integers
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Assign values
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// Resize memory to 10 integers
ptr = (int*)realloc(ptr, 10 * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// Assign new values to the extended part
for (int i = n; i < 10; i++) {
ptr[i] = i + 1;
}
// Display all values
for (int i = 0; i < 10; i++) {
printf("%d ", ptr[i]);
}
// Free allocated memory
free(ptr);
return 0;
}
Explanation:
realloc
resizes the memory from 5 integers to 10 integers.- It preserves the original data in the allocated memory and initializes new slots with values.
4. free
(Free Allocated Memory)
The free
function deallocates memory that was previously allocated with malloc
, calloc
, or realloc
. Failing to free memory when it’s no longer needed can lead to memory leaks.
Summary
malloc
: Allocates a specified amount of uninitialized memory.calloc
: Allocates memory for an array and initializes it to zero.realloc
: Resizes previously allocated memory.free
: Frees allocated memory to prevent memory leaks.