Course Content
C Tutorial
About Lesson

Dynamic Memory Allocation (malloc, calloc, realloc, free)

Dynamic memory allocation is a crucial aspect of programming in C, allowing for flexible memory management during runtime. The functions malloc, calloc, realloc, and free play significant roles in this process, facilitating the allocation and deallocation of memory as needed.

Basics of Dynamic Memory Allocation

In C, dynamic memory allocation enables the allocation of memory at runtime, unlike static allocation where memory is assigned during compile time. This flexibility is essential when the size of data structures or variables is unknown beforehand.

malloc: Allocating Memory

The malloc function stands for “memory allocation” and is used to allocate a specified number of bytes from the heap. It takes the size of memory to be allocated as an argument and returns a pointer to the allocated memory if successful, or a NULL pointer if unsuccessful.

int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers
if (ptr == NULL) {
// Memory allocation failed
// Handle the error
} else {
// Memory allocation successful
// Use the allocated memory
}
calloc: Allocating Contiguous Memory

Similar to malloc, the calloc function allocates memory, but it also initializes the allocated memory block to zero. It takes the number of elements and the size of each element as arguments.

int *ptr;
ptr = (int *)calloc(5, sizeof(int)); // Allocating memory for 5 integers, initialized to zero
if (ptr == NULL) {
// Memory allocation failed
// Handle the error
} else {
// Memory allocation successful
// Use the allocated memory
}
realloc: Resizing Allocated Memory

The realloc function is used to change the size of an already allocated memory block. It takes a pointer to the original memory block and the new size as arguments. If the reallocation is successful, it returns a pointer to the resized memory block.

int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers
if (ptr != NULL) {
ptr = (int *)realloc(ptr, 10 * sizeof(int)); // Resizing memory for 10 integers
if (ptr == NULL) {
// Memory reallocation failed
// Handle the error
} else {
// Memory reallocation successful
// Use the resized memory
}
} else {
// Memory allocation failed
// Handle the error
}
free: Deallocating Memory

The free function is used to deallocate the dynamically allocated memory. It helps prevent memory leaks by releasing the memory back to the system for reuse.

int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers
if (ptr != NULL) {
// Use the allocated memory
// ...

free(ptr); // Deallocate the memory
ptr = NULL; // Reset the pointer
} else {
// Memory allocation failed
// Handle the error
}

Best Practices and Precautions

  • Error Handling: Always check for the return value of malloc, calloc, and realloc to ensure successful memory allocation.
  • Memory Leak Prevention: Properly deallocate memory using free once it’s no longer needed.
  • NULL Pointer: Assign a pointer to NULL after deallocation to avoid dangling pointers