Calloc is a function utilized to dynamically allocate memory for variables or arrays. This memory allocation initializes the allocated memory to zero. Commonly employed in the C programming language, it is also applicable in C++.
In C++, we utilize keywords such as 'new' and 'new' for allocating memory for individual elements and arrays of elements. Another option for memory allocation in C++ is the 'calloc' function. This 'calloc' function is available in the <cstdlib> header file.
Syntax of the calloc function in C++:
It has the following syntax:
void* calloc(size_t number_of_elements, size_t size_of_elements);
Parameters passed to the calloc function are:
The
- parameter denotes the quantity of elements to reserve memory for.
An unsigned integer is the required data type for this value.
Sizeofelements:
- This specifies the dimensions of individual sections in bytes.
- The value must be a non-negative integer.
The output of the calloc function includes:
- It provides a pointer that references the beginning of the allocated memory block.
- In case of allocation failure, it returns a null pointer.
Example:
Let's consider a basic program to showcase how the calloc function is utilized in C++:
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int *ptr = (int *)calloc(5, sizeof(int));
if (!ptr) {
cout << "Memory Allocation Failed";
exit(1);
}
for (int i = 0; i < 5; i++) {
ptr[i] = i+1;
}
for (int i = 0; i < 5; i++) {
cout << ptr[i] << endl;
}
free(ptr);
return 0;
}
Output:
Explanation:
In the C++ code snippet provided above, we initially included the necessary header files such as <cstdlib> and <iostream>. Within the main function, we employed the calloc function to allocate memory, specifying 5 as the number of elements and the size of each element equivalent to the size of an integer. The return value of calloc, a pointer, was assigned to ptr. Subsequently, we validated whether ptr is a null pointer; in case it is, an error message is outputted, followed by filling the memory blocks with values. Finally, the program displayed the elements stored in memory. To release the allocated memory, we utilized the free function.
Example:
A program if calloc function with size zero;
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int *ptr = (int *)calloc(0, 0);
if (ptr == NULL) {
cout << "Null pointer";
}
cout<< ptr<< endl;
free(ptr);
return 0;
}
Output:
Explanation:
In the preceding code snippet, the calloc function was employed to dynamically assign memory. Within this context, a function was utilized to assign memory for an array devoid of any elements, each element occupying 0 bytes. The resulting output will be a hexadecimal representation denoting the memory's specific location within the memory layout. It's crucial to note that this address is contingent upon the implementation and may fluctuate across diverse systems and compilers. Additionally, the free function was called to release the dynamically allocated memory.
Example:
A code example demonstrating the functionality of the calloc function:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int **matrix1 = static_cast<int **>(calloc(rows, sizeof(int *)));
for (int i = 0; i < rows; ++i) {
matrix1[i] = static_cast<int *>(calloc(cols, sizeof(int)));
}
cout << "Enter elements for the first matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix1[i][j];
}
}
int **matrix2 = static_cast<int **>(calloc(rows, sizeof(int *)));
for (int i = 0; i < rows; ++i) {
matrix2[i] = static_cast<int *>(calloc(cols, sizeof(int)));
}
cout << "Enter elements for the second matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix2[i][j];
}
}
int **resultMatrix = static_cast<int **>(calloc(rows, sizeof(int *)));
for (int i = 0; i < rows; ++i) {
resultMatrix[i] = static_cast<int *>(calloc(cols, sizeof(int)));
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
resultMatrix[i][j] += matrix1[i][j] + matrix2[i][j];
}
}
cout << "Resultant matrix after addition:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << resultMatrix[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < rows; ++i) {
free(matrix1[i]);
free(matrix2[i]);
free(resultMatrix[i]);
}
free(matrix1);
free(matrix2);
free(resultMatrix);
return 0;
}
Output:
Explanation:
This C++ software enables users to specify the dimensions and elements of two matrices. It allocates memory for these matrices using calloc, computes the sum of the matrices, and displays the resulting matrix. Initially, it allocates memory for the matrices based on the specified dimensions. Subsequently, it prompts the user to input values to populate both matrices. Following this, it performs addition operation on corresponding elements of the matrices to produce a new matrix. Finally, it deallocates the allocated memory to prevent any memory-related problems. Essentially, this program facilitates matrix addition and visualization of the computed result.