Dynamic Memory Allocation
Example
If there is a situation where only 5 elements are
needed to be entered in this array. In this case, the
remaining 4 indices are just wasting memory in this
array. So there is a requirement to lessen the length
(size) of the array from 9 to 5.
Take another situation. In this, there is an array of 9
elements with all 9 indices filled. But there is a need
to enter 3 more elements in this array. In this case, 3
indices more are required. So the length (size) of the
array needs to be changed from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in
C.
Therefore, C Dynamic Memory Allocation can be defined as a
procedure in which the size of a data structure (like Array)
Dynamic memory allocation
• Allocating the storage/memory during the runtime.
• There are 4 library functions provided by C defined
under <stdlib.h> header file to facilitate dynamic memory
allocation in C programming. They are:
malloc()
calloc()
free()
realloc()
Malloc or memory allocation
• malloc() : Whenever a new area of memory is required malloc()
can be called. It dynamically allocate a single large block of
contiguous memory according to the size specified.
• If requested memory is available, a pointer to the start of an
area of memory of the required size is returned.
• If requested memory is not available, the pointer NULL is
returned.
• malloc() doesn’t initialize the allocated memory.
Malloc or memory allocation
• If the content of memory blocks are accessed before initialization,
then it results in segmentation fault error(or may be garbage values).
• Syntax: ptr = (cast-type*) malloc(byte-size)
• Example: ptr = (int*)malloc(n * sizeof(int));
• ptr = (int*) malloc(100 * sizeof(int));
• Since the size of int is 4 bytes, this statement will allocate 400 bytes of
memory, and the pointer ptr holds the address of the first byte in the
allocated memory.
Example program else
#include <stdio.h> {
#include <stdlib.h> printf("Memory allocation is successful.");
int main() for (i = 0; i < n; ++i)
{ ptr[i] = i + 1;
printf(“Array elements are: ");
int* ptr;
for (i = 0; i < n; ++i)
int n=5, i;
printf("%d, ", ptr[i]);
printf("Enter number of elements: %d", n);
}
ptr = (int*)malloc(n * sizeof(int));
return 0;
if (ptr == NULL) }
{
printf("Memory not allocated.\n");
exit(0);
calloc() or contiguous allocation
• Method in C is used to dynamically allocate multiple blocks of memory,
the specified number of blocks of memory of the specified type.
• It initializes each block with a default value ‘0’.
• Syntax : ptr = (cast-type*)calloc(n, element-size);
• ptr = (int*)calloc(n, sizeof(int));
• Example: ptr = (float*) calloc(25, sizeof(float));
• This statement allocates contiguous space in memory for
25 elements each with the size of the float.
Example program else
#include <stdio.h>
{
#include <stdlib.h>
int main() printf("Memory Allocation
{ Successful");
int* ptr; for (i = 0; i < n; ++i)
int n=5, i; ptr[i] = i + 1;
printf("Enter number of elements: %d", n); printf(“Array elements are: ");
ptr = (int*)calloc(n , sizeof(int)); for (i = 0; i < n; ++i)
if (ptr == NULL)
printf("%d, ", ptr[i]);
{
printf("Memory not allocated.\n"); }
exit(0); return 0;
} }
realloc
• Size of dynamically allocated memory can be changed by using realloc() (realloc() resizes
the memory block that was previously allocated with a call to malloc or calloc.)
• ptr = (int*) malloc(100 * sizeof(int));
• p = (int*)realloc(ptr, n*sizeof(int));
• realloc deallocates the old object pointed to by ptr and returns a pointer to a new object
that has the size specified.
• The contents of the new object is identical to that of the old object prior to deallocation,
up to the lesser of the new and old sizes.
• Any bytes in the new object beyond the size of the old object have
indeterminate values.
Example program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30; Output:
10 20 30
for(i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
getchar();
return 0;
Free method
• “free” method in C is used to dynamically de-allocate the memory
previously allocated by malloc() and calloc(), since it is not de-allocated on
their own.
• Hence the free() method is used, whenever the dynamic memory
allocation takes place.
• It helps to reduce wastage of memory by freeing it.
• Casting of argument is avoided in the call to free.
• Syntax: free(ptr);
Example program
#include <stdio.h>
else
#include <stdlib.h> {
int main() printf("Malloc Allocation is Success");
{ int *ptr, *ptr1, int n, i; free(ptr);
printf("Enter number of elements: printf("Malloc Memory successfully freed.");
%d", n); printf("Calloc Allocation is Success.");
ptr = (int*)malloc(n * sizeof(int)); free(ptr1);
ptr1 = (int*)calloc(n, sizeof(int)); printf(“Calloc Memory successfully freed.");
if (ptr == NULL || ptr1 == NULL) }
return 0;
{ printf("Memory not
allocated.\n"); }
exit(0);
}
Difference between static and
dynamic memory allocation