Library functions for Dynamic memory allocation
- Siddharth Sharma
- Dec 20, 2024
- 3 min read
In C programming, managing memory efficiently is crucial, especially when the exact amount of memory required is unknown during compilation. This is where dynamic
memory allocation comes into play. Let's explore what it means and how to use it with library functions provided in C.
What is Dynamic Memory Allocation?
Dynamic memory allocation allows you to request memory at runtime, meaning you can allocate memory as needed while the program is running. This is different from static memory allocation, where the memory size is fixed during compilation.
The C Standard Library provides functions for dynamic memory management, which are declared in the <stdlib.h> header file. These functions allow you to:
Allocate memory dynamically.
Resize allocated memory.
Free unused memory.
Key Functions for Dynamic Memory Allocation
Here are the main functions used for dynamic memory management in C:
malloc (Memory Allocation)
Purpose: Allocates a specified number of bytes and returns a pointer to the first byte of the allocated memory.
Syntax:
void* malloc(size_t size);
Example:
int* ptr = (int*) malloc(10 * sizeof(int)); if (ptr == NULL) { printf("Memory allocation failed!\n"); }
This allocates memory for an array of 10 integers.
calloc (Contiguous Allocation)
Purpose: Allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer to the allocated memory.
Syntax:
void* calloc(size_t num, size_t size);
Example:
int* ptr = (int*) calloc(10, sizeof(int)); if (ptr == NULL) { printf("Memory allocation failed!\n"); }
This allocates and zero-initializes memory for an array of 10 integers.
realloc (Reallocation)
Purpose: Resizes a previously allocated memory block to a new size.
Syntax:
void* realloc(void* ptr, size_t newSize);
Example:
ptr = (int*) realloc(ptr, 20 * sizeof(int)); if (ptr == NULL) { printf("Memory reallocation failed!\n"); }
This resizes the memory block to hold 20 integers.
free (Deallocation)
Purpose: Frees memory that was previously allocated using malloc, calloc, or realloc.
Syntax:
void free(void* ptr);
Example:
free(ptr); ptr = NULL; // Prevent dangling pointer
Important Points to Remember
Always check for NULL: After calling malloc, calloc, or realloc, check if the pointer returned is NULL. A NULL pointer means the allocation failed.
Free allocated memory: Always use free to release memory when it’s no longer needed. This prevents memory leaks.
Avoid dangling pointers: After freeing memory, set the pointer to NULL to avoid accessing invalid memory.
Use sizeof correctly: When allocating memory, use the sizeof operator to determine the correct size of the data type.
Example: Dynamic Memory Allocation in Action
Here’s a complete example to illustrate the use of these functions:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Dynamically allocate memory using malloc
int* arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Input elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Display elements
printf("You entered: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free allocated memory
free(arr);
return 0;
}Conclusion
Dynamic memory allocation is a powerful tool that gives you flexibility in managing memory in C programs. By mastering the use of malloc, calloc, realloc, and free, you can write more efficient and robust programs. Remember to handle memory responsibly to avoid common pitfalls like memory leaks and segmentation faults. Happy coding!




Comments