Dynamic memory allocation is a crucial aspect of programming in C, allowing the allocation and deallocation of memory during program execution. Unlike static memory allocation, where memory is allocated at compile-time and cannot be changed during runtime, dynamic memory allocation enables flexibility by allocating memory dynamically during program execution.
In C, dynamic memory allocation is managed through functions from the standard library <stdlib.h>
, primarily malloc()
, calloc()
, realloc()
, and free()
.
malloc()
: Allocates a block of memory of specified size and returns a pointer to the beginning of the block.calloc()
: Allocates a block of memory for an array of elements, initializes them to zero, and returns a pointer to the beginning of the block.realloc()
: Resizes the previously allocated memory block, either expanding or shrinking it. It returns a pointer to the resized memory block.free()
: Deallocates the memory previously allocated by malloc()
, calloc()
, or realloc()
.Let’s consider an example to understand the usage of malloc()
#include
#include
int main() {
int *ptr;
int n = 5;
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Memory allocated successfully\n");
// Use the allocated memory
free(ptr); // Free the allocated memory
return 0;
}
ptr
to store the address of the allocated memory.n
represents the number of elements we want to allocate memory for.malloc()
allocates memory for n
integers (in bytes) and returns a pointer to the first byte of the allocated memory.NULL
.free()
function.Now, let’s see how to use the calloc()
function
#include
#include
int main() {
int *ptr;
int n = 5;
ptr = (int *)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Memory allocated successfully\n");
// Use the allocated memory
free(ptr); // Free the allocated memory
return 0;
}
calloc()
allocates memory for n
integers (in bytes) and initializes them to zero.malloc()
.The realloc()
function is used to resize an already allocated block of memory. Here’s how it works
#include
#include
int main() {
int *ptr;
int n = 5;
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Memory allocated successfully\n");
// Reallocate memory for 10 integers
ptr = (int *)realloc(ptr, 10 * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
printf("Memory reallocated successfully\n");
// Use the reallocated memory
free(ptr); // Free the allocated memory
return 0;
}
n
integers using malloc()
.realloc()
.NULL
.Dynamic memory allocation in C is a powerful feature that allows programs to manage memory dynamically during runtime, providing flexibility and efficiency. By understanding the functions malloc(), calloc(), realloc(), and free(), programmers can effectively manage memory allocation in their C programs. However, it's important to use dynamic memory allocation judiciously to avoid memory leaks and segmentation faults.Happy coding!❤️