Memory Management in C: free and realloc
Introduction
In C programming, dynamic memory allocation is a crucial concept that allows programs to allocate memory at runtime. This is particularly useful when the size of data structures like arrays is not known beforehand. The malloc, calloc, realloc, and free functions are commonly used for dynamic memory management. This tutorial will focus on the free and realloc functions.
The free Function
The free function is used to deallocate memory that was previously allocated by malloc, calloc, or realloc. It helps in preventing memory leaks by releasing memory that is no longer needed.
Syntax:
Here, ptr is a pointer to the memory block that needs to be freed. If ptr is NULL, no operation is performed.
#include <stdlib.h>
#include <stdio.h>
int main() {
int *ptr = (int*)malloc(10 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
free(ptr); // Deallocate the memory
return 0;
}
The realloc Function
The realloc function is used to resize a previously allocated memory block. It can extend or shrink the memory block while preserving the contents.
Syntax:
Here, ptr is a pointer to the memory block that needs to be resized, and new_size is the new size in bytes. If new_size is zero, realloc behaves like free and deallocates the memory block.
#include <stdlib.h>
#include <stdio.h>
int main() {
int *ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
// Resize the allocated memory
ptr = (int*)realloc(ptr, 10 * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// Use the resized memory
free(ptr); // Deallocate the memory
return 0;
}
Example: Using free and realloc Together
Let's consider an example where we use both free and realloc functions together to manage memory dynamically for an integer array.
#include <stdlib.h>
#include <stdio.h>
int main() {
int *array = (int*)malloc(5 * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Initialize the array
for (int i = 0; i < 5; i++) {
array[i] = i + 1;
}
// Print the array
printf("Original array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
printf("\n");
// Resize the array
array = (int*)realloc(array, 10 * sizeof(int));
if (array == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// Initialize the new elements of the array
for (int i = 5; i < 10; i++) {
array[i] = i + 1;
}
// Print the resized array
printf("Resized array: ");
for (int i = 0; i < 10; i++) {
printf("%d ", array[i]);
}
printf("\n");
// Free the allocated memory
free(array);
return 0;
}
Conclusion
Proper memory management is crucial in C programming to avoid memory leaks and ensure efficient use of resources. The free function is used to deallocate memory that is no longer needed, while the realloc function is used to resize an existing memory block. Understanding how to use these functions effectively can help in writing robust and efficient C programs.
