C Dynamic Memory – Using malloc, calloc, realloc, and free
In C, dynamic memory allocation allows you to allocate memory at runtime instead of at compile time. This is useful when you donโt know the amount of memory needed beforehand or when you want to manage memory more efficiently during program execution. The main functions used for dynamic memory allocation are malloc()
, calloc()
, realloc()
, and free()
.
๐น malloc() – Memory Allocation
The malloc()
function is used to allocate a block of memory of a specified size. It returns a pointer to the first byte of the allocated memory block, or NULL
if the allocation fails.
๐ Example 1: Using malloc()
Here, we allocate memory for an integer array dynamically using malloc()
and then free the memory after use.
#include <stdio.h> #include <stdlib.h> int main() { int *arr; arr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } // Assigning values to the allocated memory for (int i = 0; i < 5; i++) { arr[i] = i + 1; } // Printing values for (int i = 0; i < 5; i++) { printf("arr[%d] = %d\n", i, arr[i]); } free(arr); // Freeing the allocated memory return 0; }
๐น calloc() - Contiguous Allocation
The calloc()
function allocates memory for an array of elements, initializing all bytes to zero. It takes two arguments: the number of elements and the size of each element.
๐ Example 2: Using calloc()
#include <stdio.h> #include <stdlib.h> int main() { int *arr; arr = (int *)calloc(5, sizeof(int)); // Allocating memory for 5 integers, initialized to zero if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } // Printing the values (all should be 0) for (int i = 0; i < 5; i++) { printf("arr[%d] = %d\n", i, arr[i]); } free(arr); // Freeing the allocated memory return 0; }
๐น realloc() - Reallocation
The realloc()
function is used to resize a previously allocated memory block. It can either extend or shrink the size of the memory block.
๐ Example 3: Using realloc()
This example demonstrates how to resize an array dynamically using realloc()
.
#include <stdio.h> #include <stdlib.h> int main() { int *arr; arr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers // Resize the memory block to hold 10 integers arr = (int *)realloc(arr, 10 * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } // Assigning new values to the resized memory for (int i = 0; i < 10; i++) { arr[i] = i + 1; } // Printing the values for (int i = 0; i < 10; i++) { printf("arr[%d] = %d\n", i, arr[i]); } free(arr); // Freeing the allocated memory return 0; }
๐น free() - Memory Deallocation
The free()
function is used to deallocate previously allocated memory, preventing memory leaks in your program.
๐ Example 4: Using free()
In this example, memory is allocated using malloc()
, and then it is freed using free()
.
#include <stdio.h> #include <stdlib.h> int main() { int *arr; arr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } // Assigning values and then freeing the memory free(arr); printf("Memory has been freed\n"); return 0; }
โ๏ธ Common Use Cases
- Managing memory when the size of data structures is not known at compile time.
- Efficiently resizing arrays as your program grows or shrinks in size.
- Using dynamic memory in scenarios like linked lists or trees.
๐ฏ Recap
Dynamic memory management is a crucial part of C programming, giving you the flexibility to allocate and deallocate memory as needed. Functions like malloc()
, calloc()
, realloc()
, and free()
allow for efficient memory use and help you avoid memory leaks when used correctly.