Dynamic Memory Allocation (DMA)
- Siddharth Sharma
- Dec 19, 2024
- 3 min read
Dynamic Memory Allocation (DMA) refers to the process of allocating memory during the execution of a program (runtime) rather than when the program is compiled. This approach provides flexibility, allowing programs to efficiently use memory based on their needs.
Why Dynamic Memory Allocation is Important
Flexible Memory Usage: You can request memory as needed and release it when it’s no longer required.
Efficient Utilization: Prevents wastage by allocating memory dynamically rather than using a fixed size.
Scalability: Ideal for situations where the size of data is unknown or may change during program execution.
Memory Areas in a Program
Stack: Stores local variables and function calls. The size is fixed and allocated during compile time.
Heap: Used for dynamically allocated memory. The size can grow or shrink during runtime, making it more flexible.
Functions for Dynamic Memory Allocation (C Language)
In C, dynamic memory allocation is managed using the <stdlib.h> library, which provides the following functions:
malloc(size_t size)
Allocates a block of memory of the specified size (in bytes).
Returns a pointer to the allocated memory or NULL if allocation fails.
The memory is uninitialized.
Example:
int *arr = (int *)malloc(5 * sizeof(int)); // Allocates memory for 5 integerscalloc(size_t num, size_t size)
Allocates memory for an array of num elements, each of size bytes.
Initializes all elements to zero.
Returns a pointer to the allocated memory or NULL if allocation fails.
Example:
int *arr = (int *)calloc(5, sizeof(int)); // Allocates and initializes memory for 5 integersrealloc(void *ptr, size_t new_size)
Resizes an existing memory block to the new size.
Can move the memory block to a new location if required.
Example:
arr = (int *)realloc(arr, 10 * sizeof(int)); // Resizes memory to hold 10 integersfree(void *ptr)
Frees the previously allocated memory block.
Prevents memory leaks by returning memory to the system.
Example:
free(arr); // Deallocates memoryDynamic Multi-Dimensional Arrays
Dynamic memory allocation is not limited to one-dimensional arrays. You can also allocate memory for multi-dimensional arrays dynamically, offering flexibility in defining arrays with variable sizes.
Allocating 2D Arrays
To create a 2D array dynamically, you can allocate memory for an array of pointers and then allocate memory for each row.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
int **matrix;
// Allocate memory for row pointers
matrix = (int **)malloc(rows * sizeof(int *));
if (matrix == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Allocate memory for each row
for (int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
printf("Memory allocation failed for row %d!\n", i);
return 1;
}
}
// Initialize and display the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = i + j;
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Free allocated memory
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}Allocating 3D Arrays
For 3D arrays, follow a similar approach by first allocating memory for an array of 2D arrays and then for each 2D slice.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = 3, y = 4, z = 5;
int ***cube;
// Allocate memory for 2D array pointers
cube = (int ***)malloc(x * sizeof(int **));
if (cube == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Allocate memory for each 2D array
for (int i = 0; i < x; i++) {
cube[i] = (int **)malloc(y * sizeof(int *));
if (cube[i] == NULL) {
printf("Memory allocation failed for layer %d!\n", i);
return 1;
}
// Allocate memory for each row in the 2D array
for (int j = 0; j < y; j++) {
cube[i][j] = (int *)malloc(z * sizeof(int));
if (cube[i][j] == NULL) {
printf("Memory allocation failed for row %d in layer %d!\n", j, i);
return 1;
}
}
}
// Initialize and display the 3D array
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
cube[i][j][k] = i + j + k;
printf("%d ", cube[i][j][k]);
}
printf("\n");
}
printf("\n");
}
// Free allocated memory
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
free(cube[i][j]);
}
free(cube[i]);
}
free(cube);
return 0;
}



Comments