Dynamic memory allocation in C
- You know very well that an array is a set of a fixed number of values. Once the size of the collection has been declared, you can not change it.
- The size of the collection you announced can sometimes be inadequate. You can assign the memory manually during run-time to solve this problem. This is known in C programming as the dynamic memory allocation.
- The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at run-time. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
- malloc()
- calloc()
- realloc()
- free()
- Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.
C malloc()
- The name "malloc" stands for memory allocation.
- The malloc() function reserves a block of memory of the specified number of bytes.
- And, it returns a pointer of void which can be casted into pointers of any form.
Syntax of malloc()
ptr = (castType*) malloc(size);
Example
ptr = (float*) malloc(100 * sizeof(float));
- The statement above sets aside 400 bytes of memory. That's because float size is 4 bytes.
- And the pointer ptr retains in the allocated memory the address of the first Bit.
- If the memory can not be allocated, then the expression results in a NULL point.
C calloc()
- The name "calloc" stands for contiguous allocation.
- The malloc() function allocates memory and leaves the memory uninitialized.
- Whereas, the calloc() function allocates memory and initializes all bits to zero.
Syntax of calloc()
ptr = (castType*)calloc(n, size);
Example:
ptr = (float*) calloc(25, sizeof(float));
- The above statement allocates contiguous space in memory for 25 elements of type float.
C free()
- Memory dynamically allocated generated with either calloc or malloc, will not be released on its own.
- To unlock space you will have to use directly free.
Syntax of free()
free(ptr);
- This statement frees the space allocated in the memory pointed by ptr.
Example of malloc() and free() function
// Program to calculate the sum of n numbers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}
- Here, we have dynamically allocated the memory for n number of int.
Example of calloc() and free()
// Program to calculate the sum of n numbers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
C realloc()
- If the dynamically allocated memory is insufficient or more than required, you can change the size of previously allocated memory using the realloc() function.
Syntax of realloc()
ptr = realloc(ptr, x);
- ptr is reallocated with a new size x.
Example of realloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
// rellocating the memory
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory: ");
for(i = 0; i < n2; ++i)
printf("%u\n", ptr + i);
free(ptr);
return 0;
}
Expected output
Enter size: 5
Addresses of previously allocated memory:26855472
26855476
Enter the new size: 8
Addresses of newly allocated memory:26855472
26855476
26855480
26855484