Dynamic memory allocation
Dynamic memory allocation is an essential concept in C programming. It allows a programmer to allocate custom size of memory in runtime, which can be manipulated using four functions: malloc(), calloc(), realloc(), and free(). In this post, we will discuss these four functions in detail.
1. malloc()
The malloc() function is used to allocate some space in the heap memory. The syntax for using the malloc() function is:
copy
ptr = (int *) malloc(size);
Here, ptr is a pointer to the first byte of the allocated memory block, and size is the number of bytes to be allocated.
Let's take an example to understand how to use the malloc() function:
copy
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
// Allocate memory for 5 integers
ptr = (int *) malloc(5 * sizeof(int));
printf("Enter 5 numbers\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", ptr + i);
}
printf("You entered: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
// Free the allocated memory
free(ptr);
return 0;
}
2. calloc()
The calloc() function is similar to the malloc() function, but it also initializes the allocated memory to zero. The syntax for using the calloc() function is:
copy
ptr = (int *) calloc(n, size);
Here, ptr is a pointer to the first byte of the allocated memory block, n is the number of elements to be allocated, and size is the size of each element in bytes.
Let's take an example to understand how to use the calloc() function:
copy
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
// Allocate memory for 5 integers and initialize to 0
ptr = (int *) calloc(5, sizeof(int));
printf("Enter 5 numbers\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", ptr + i);
}
printf("You entered: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
// Free the allocated memory
free(ptr);
return 0;
}
3. realloc()
The realloc() function is used to reallocate the memory that was previously allocated using the malloc() or calloc() function. The syntax for using the realloc() function is:
copy
ptr = (int *) realloc(ptr, size);
Here, ptr is the pointer to the previously allocated memory block, and size is the new size of the memory block.
Let's take an example to understand how to use the realloc() function:
copy
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
// allocate memory for an array of 5 integers
ptr = (int *)calloc(5, sizeof(int));
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", ptr + i);
}
// increase the size of the array to 10 integers
ptr = (int *)realloc(ptr, 10 * sizeof(int));
printf("Enter 5 more numbers:\n");
for (int i = 5; i < 10; i++)
{
scanf("%d", ptr + i);
}
printf("The final array is:\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", *(ptr + i));
}
// free the allocated memory
free(ptr);
return 0;
}
The function free() is used to deallocate the memory block that was previously allocated by malloc(), calloc() or realloc(). It releases the memory back to the operating system, so that it can be used for other purposes.
Overall, these memory management functions are essential in C programming for efficient use of memory and avoiding memory leaks.