Dynamic Memory Allocation in C Language:
Dynamic Memory Allocation
Dynamic memory allocation allows you to allocate memory at runtime. This is particularly useful when the amount of memory needed cannot be determined at compile time. The standard library provides functions to allocate and free memory dynamically.
Memory Allocation Functions
malloc()
: Allocates a block of memory.
calloc()
: Allocates a block of memory for an array of elements and initializes all bytes to zero.
realloc()
: Reallocates a previously allocated block of memory to a new size.
free()
: Frees a previously allocated block of memory.
malloc()
Function
The malloc()
function allocates a block of memory of the specified size and returns a pointer to the beginning of the block. If the allocation fails, it returns NULL
.
#include
#include
int main() {
int *ptr;
int n = 5;
// Allocate memory for 5 integers
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// Print the allocated memory
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Free the allocated memory
free(ptr);
return 0;
}
calloc()
Function
The calloc()
function allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer to the first byte of the allocated memory. If the allocation fails, it returns NULL
.
#include
#include
int main() {
int *ptr;
int n = 5;
// Allocate memory for 5 integers and initialize to zero
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Print the allocated memory
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Free the allocated memory
free(ptr);
return 0;
}
realloc()
Function
The realloc()
function reallocates a previously allocated block of memory to a new size. If the reallocation fails, it returns NULL
and the original block of memory is left unchanged.
#include
#include
int main() {
int *ptr;
int n = 5;
// Allocate memory for 5 integers
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// Reallocate memory for 10 integers
n = 10;
ptr = (int*)realloc(ptr, n * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// Use the reallocated memory
for (int i = 5; i < n; i++) {
ptr[i] = i + 1;
}
// Print the reallocated memory
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Free the allocated memory
free(ptr);
return 0;
}
free()
Function
The free()
function deallocates a previously allocated block of memory, making it available for future allocations.
#include
#include
int main() {
int *ptr;
int n = 5;
// Allocate memory for 5 integers
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// Print the allocated memory
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Free the allocated memory
free(ptr);
return 0;
}