“calloc” is yet another commonly used function of the “stdlib.h” library. It stands for contiguous allocation. As the name says, this function is also used to allocate memory while using the heap instead of the stack. Again, this kind of memory allocation is known as dynamic memory allocation. The main aim of today’s discussion is to throw light on how the “calloc” function works in C. Then, we will draw a comparison between the “calloc” and “malloc” functions. Finally, we will elaborate on the usage of the “calloc” function in C by sharing an example with you.
Working of “calloc” in C:
The “calloc” function is utilized to allocate dynamic memory at run time. That is why it can easily cater to the changing memory demands at the time of execution. The basic syntax of the “calloc” function in C as follows:
Here, “typecast” represents the type of pointer to be returned. The “num” represents the number of contiguous memory blocks that you want to reserve whereas “size” refers to the type of data that will be assigned to your dynamically allocated memory.
“calloc” vs. “malloc”:
The working of the “calloc” function is very much similar to the “malloc” function. However, the main difference lies in the ways in which the allocation of the memory takes place in both of these functions. Whenever you allocate memory with the help of the “malloc” function, it does not initialize the allocated memory by default. It means that if you will try to access the allocated memory before initialization, then you will get garbage values in return.
On the other hand, the “calloc” function initializes the allocated memory locations with “0”. Therefore, even if you will try to access those memory locations before the actual initialization, even then you will get zeros instead of garbage values. As a general rule, if you need your allocated memory locations to be initialized with zeros for any special reason, then it is better to use the “calloc” function. Otherwise, you can use the “malloc” function because the “malloc” function does not do any default initialization that is why it is a little faster than the “calloc” function.
After understanding the difference between the “calloc” and “malloc” functions in C, let us go through an example of using the “calloc” function in C.
Example of Using “calloc” in C:
For using this function in C, we have devised a simple C program as shared in the following image. First, we have declared an integer “n” and an integer pointer “ptr”. Here, “n” represents the number of elements that you want for your dynamic array. Then we have taken this “n” as input from the user. After that, we have declared our “calloc” function for allocation “n” blocks of integer type dynamic memory.
When all the declaration is done, we have an “if” statement in which we want to check if our pointer is NULL. If it is NULL, then our code will exit with an error message. If it is not NULL, then the “else” statement will be executed in which we have first printed a “successful memory allocation” message. After that, we have taken the values of our dynamic array as input from the user.
Once our dynamic array is populated. We’ve printed its values on the terminal with another “for” loop. Finally, we have wrapped up our code with a “free” function for releasing the dynamic memory that we had acquired in this program followed by the “return” statement.
Now, it is time to compile this code with the command stated below:
After a successful compilation, you can execute the code with the below-cited command:
When we will execute this code, it will ask us to enter the number of elements of our dynamic array. We have entered number “3” as we wanted three different values. This is shown in the image below:
Then, the code will ask us to enter those values. We have entered the values, “1”, “2”, and “3” respectively as shown in the following image:
Once we will enter all those values, our code will print them on the terminal as shown in the image below:
Now, we just tweaked our code a little bit so that we can verify if “calloc” actually initializes the memory locations with a “0” or not. For that, we have modified our above code in such a way that it only declares the “calloc” function and does not take any values as input. Right after the declaration of this function, we have tried to print the values of these memory locations as shown in the following image:
This time, when you will execute this code, you will be asked to enter the number of contiguous memory locations that you want. We have entered “5” as shown in the image below:
As soon as we will tap the enter key, the values of the 5 memory locations of our dynamic array will be shown on the terminal. In this scenario, all the values will be “0” as shown in the following image. This confirms that the “calloc” function initializes your allocated dynamic memory with zeros.
Hopefully, after going through this article, you will be able to clearly distinguish between the “calloc” and “malloc” functions in the C programming language. Although these functions can be used interchangeably, it is highly recommended for you to first identify the kind of initialization that you want. Only then, you should decide which of these functions would be suitable for your specific case so that you can make your C code more robust and efficient.