Dynamic Memory Allocation
Prepared By : Mashiat Mustaq
Edited By: Md. Nurul Muttakin
Why do we need dynamic memory allocation in C?
● Increase or decrease the size of an array
● Waste of memory
Size : 5 1 2 3 4 5
Increase to 7 1 2 3 4 5 6 7
Decrease to 4 1 2 3 4
Dynamic Memory Allocation
● Size of a data structure(such as Array) is changed during runtime
● Header file : stdlib.h
● 4 library functions
○ malloc()
○ calloc()
○ free()
○ realloc()
Dynamic Memory Allocation : malloc()
● dynamically allocate a single large block of memory with the
specified size
● returns a pointer of type void
● ptr = (cast-type*) malloc(byte-size)
● Example : ptr = (int*) malloc(100 * sizeof(int));
Dynamic Memory Allocation : malloc()
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
}
Dynamic Memory Allocation : calloc()
● Same as malloc() with two differences:
○ Initializes each block with a default 0
○ Takes two parameters
● ptr = (cast-type*)calloc(n, element-size);
● Example : ptr = (float*) calloc(25,
sizeof(float));
Dynamic Memory Allocation : calloc()
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
}
Dynamic Memory Allocation : free()
● dynamically de-allocate the memory so it can be reused
● free(ptr);
// heap overflow by continuously allocating memory
for (int i=0; i<10000000; i++)
{
// Allocating memory without freeing it
int *ptr = (int *)malloc(sizeof(int));
}
Dynamic Memory Allocation : malloc() and free()
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
}
free(ptr);
Dynamic Memory Allocation : realloc()
● dynamically re-allocate memory allocated by malloc(), calloc()
● maintains the already present value and new blocks will be
initialized with the default garbage value.
● ptr = realloc(ptr, newSize);
Dynamic Memory Allocation : realloc()
int* ptr; n = 10;
int n, i;
n = 5; ptr = (int*)realloc(ptr, n *
ptr = (int*)calloc(n, sizeof(int)); sizeof(int));
if (ptr == NULL) {
printf("Memory not for (i = 5; i < n; ++i) {
allocated.\n"); ptr[i] = i + 1;
exit(0); }
}
else {
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
ptr[i] = i + 1; printf("%d, ", ptr[i]);
} }
for (i = 0; i < n; ++i) { free(ptr);
printf("%d, ", ptr[i]); }
}
Dynamic Memory Allocation : for 2D Array
int **array; printf("Enter the elements of the 2D
int rows, cols, i, j; array:\n");
scanf("%d", &rows); for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array = (int**) malloc(rows * sizeof(int*)); scanf("%d", &array[i][j]);
if (array == NULL) { }
printf("Memory allocation failed!"); }
return -1;
} printf("2D array elements:\n");
for (i = 0; i < rows; i++) { for (i = 0; i < rows; i++) {
scanf("%d", &cols); for (j = 0; j < cols; j++) {
array[i] = (int*) malloc(cols * printf("%d ", array[i][j]);
sizeof(int)); }
if (array[i] == NULL) { printf("\n");
printf("Memory allocation failed!"); }
return -1;
}
}
Dynamic VS Static memory allocation
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated during program Memory is allocated during runtime.
compilation.
You cannot reuse allocated memory. You can reuse allocated memory after
releasing memory using free() function.
No library functions are required to Requires Library functions like malloc(),
allocate memory. calloc(), realloc() to allocate memory at
runtime.
We cannot modify size of allocated We can modify size of allocated
memory. memory using realloc().