C++

What is calloc() Function in C++?

Understanding the various types of memory allocation functions is critical while working in C++. The calloc() is one such function that is frequently used in C++ to dynamically allocate memory. While its use may appear simple, there are certain significant aspects to keep in mind while using calloc().

In this article, we will look at the calloc() function and how it can be utilized successfully in C++.

What is calloc() Function?

The calloc() function is a memory allocation function in C++ programming. It is used to allocate a block of memory for an array or set of variables. The calloc() function is like the malloc() function, except it also sets the allocated memory to zeroes initially. This ensures that the allocated memory is initialized to a known value, which is useful when working with arrays and structures.

The syntax for the calloc() function is as follows:

void *calloc(size_t nmemb, size_t size)

The function takes two arguments: nmemb and size. The nmemb argument specifies the number of elements in the array being allocated, while the size argument specifies the size of each element in bytes.

Also, if the memory allocation succeeds, the calloc() function returns a null pointer, else it returns a pointer to the beginning of the newly formed memory block. To avoid memory leaks, the memory allocated by calloc() should be removed using the free() method once it is no longer required.

How to Use calloc() Function in C++?

To use calloc() function in C++, follow the below-given steps:

Step 1: Include the necessary header file for calloc() function, which is <cstdlib>.

Step 2: Then declare a pointer variable of the desired data type.

Step 3: After that use the calloc() function to allocate memory and initialize it to 0.

Step 4: Use the allocated memory as required.

Step 5: Once you are done using the allocated memory, free it using the free() function.

The following example shows the calloc() function implementation in C++.

#include <cstdlib>

#include <iostream>

using namespace std;

int main() {

  int *ptr;

ptr = (int *)calloc(6, sizeof(int));

  if (!ptr) {

cout << "Memory Allocation Failed";

exit(1);

  }

cout << "Initializing values..." << endl

<< endl;

  for (int i = 0; i < 6; i++) {

ptr[i] = i * 3;

  }

cout << "Initialized values." << endl;

  for (int i = 0; i < 6; i++) {

cout << *(ptr + i) << endl;

  }

  free(ptr);

  return 0;

}

In the above code, an integer pointer ptr is declared first. The memory is then allocated for 6 integers using calloc(), with the memory being initialized with 0. The sizeof(int) function is used to calculate how much memory is required to store integers. The memory allocation is then checked for errors to ensure success. If not, the program terminates and an error message is shown.

The allocated memory is then initialized using numbers obtained by multiplying ‘i*3’ using a for loop. The values kept in the allocated memory are printed using a second for loop and pointer arithmetic with *(ptr + i). Ultimately, the memory that was dynamically allocated is released by calling the free() function, which stops memory leaks. As a sign that the program has run successfully, it returns 0.

Output

Conclusion

In C++, the calloc() function is useful for dynamically allocating memory. It not only allocates memory but also zeros it out, which is useful when working with arrays and structures. Understanding how to utilize calloc() effectively is crucial for avoiding memory leaks and ensuring program execution success. Developers can securely utilize the calloc() method in their C++ programs by following the instructions given in this article.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.