Home » Calloc in C

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.

Calloc in C

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

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

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

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

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

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  

You may also like