Calloc in C
This topic will discuss how to create dynamic memory allocation using the calloc() function in the C programming language. Before going through the concepts, let's discuss the dynamic memory allocation in C. Dynamic memory is a structure programming procedure that allows users to allocate the memory at the run time of a program. Using dynamic memory allocation, we can increase or decrease the memory during the execution of a program. In this way, it avoids the wastage of computer memory. A memory allocation is divided into two parts are malloc() and calloc() function.

A calloc() function is a predefined library function that stands for contiguous memory allocation. A calloc() function is used to create multiple blocks at the run time of a program having the same size in the memory. A calloc function is defined inside the stdlib.h header file. It has two parameters, no. of blocks and the size of each block. When the dynamic memory is allocated using the calloc() function, it returns the base address of the first block, and each block is initialized with 0. And if memory is not created, it returns a NULL pointer.
For example, suppose we want to create three blocks of memory using the calloc() function, we need to pass two parameters, a number of blocks (3) and the size of each block (int, char, float, etc.) in the byte. In this way, it creates three blocks whose size is the same inside the computer memory.
Syntax:-
ptr = (cast_type *) calloc ( number_of_blocks, size_of_block);
In the above syntax, the calloc() function has two parameters. The first parameter defines the number of blocks and the second parameter defines the size of each block in memory. The size of the blocks and cast_type can be in int, char, float, etc.
Return: It returns the base address of the first block to the ptr variable.
Program to check dynamic memory is allocated using calloc() function
Let's write a simple program to check whether the dynamic memory is allocated in C.
program.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
/* use calloc() function to define the no. of blocks and size of each blocks. */
ptr = calloc (4, sizeof(int)); // here 4 is the no. of block and int is the size of block
if (ptr != NULL)
{
printf (" Memory is created successfully \n");
}
else
printf (" Memory is not created ");
return 0;
}
Output:
Memory is created successfully
Program to demonstrate the use of the calloc() function
Let's consider creating dynamic memory allocation using calloc() function and storing data into the memory blocks.
Program2.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n, *ptr, *p, i, sum = 0;
/* n = number of elements, *ptr = store base address of the dynamic memory,
*p store temporary address of the *ptr */
printf (" Enter the number of elements: ");
scanf (" %d", &n); // it takes number of elements
// use calloc syntax to create memory block of int data type
ptr = (int *) calloc (n, sizeof(int));
p = ptr; // assign the address of ptr
if (ptr == NULL) // it checks whether the memory is allocated
{
printf (" Memory is not allocated. ");
exit(0); // exit from the program
}
printf (" Enter %d numbers \n", n);
for ( i = 1; i <= n; i++)
{
scanf ( "%d", ptr);
sum = sum + *ptr;
ptr++;
}
printf (" Elements are: ");
for (i = 1; i <= n; i++)
{
printf (" %d", *p);
p++;
}
printf (" \n The addition of the elements is: %d ", sum);
getch();
}
Output:
Enter the number of elements: 5
Enter 5 numbers
1
2
3
4
5
Elements are: 1 2 3 4 5
The addition of the elements is: 15
Program to release dynamic memory allocation using free() function
free() function: A free() function is used to release the dynamic memory which is created either calloc() or malloc() function. These allocated memories cannot be freed to their own, and they will exist till the end of the program. So, it is our responsibility to release that memory that can be reused, and hence we explicitly use the free() function to release the memory.
Syntax:-
free (ptr);
Here free() is a function that releases the allocated memory using the pointer ptr variable.
Let's consider creating dynamic memory allocation using the calloc() function and then releasing occupied space using the free() function in the C program.
Release.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n, *ptr, *p, i, sum = 0;
printf (" Define the number of elements to be entered: ");
scanf (" %d", &n);
// use calloc syntax to create memory block of int data type
ptr = (int *) calloc (n, sizeof(int));
p = ptr; // store the base address in p
if (ptr == NULL)
{
printf (" Out of memory ");
exit(0);
}
printf (" Enter the elements \n", n);
for ( i = 1; i <= n; i++)
{
scanf ( "%d", ptr);
sum = sum + *ptr;
ptr++;
}
printf (" Elements are: ");
for (i = 1; i <= n; i++)
{
printf (" %d", *p);
p++;
}
printf (" \n The addition of the elements is: %d ", sum);
free(ptr); /* Use free() function to release the dynamic memory allocation */
getch();
}
Output:
Define the number of elements to be entered: 6
Enter the elements
2
4
6
8
10
12
Elements are: 2 4 6 8 10 12
The addition of the elements is: 42