C calloc() Function
The calloc()
function in C is used to dynamically allocate memory for an array while automatically initializing all allocated bits to zero, ensuring that the memory is in a predictable state before use.
Syntax of calloc()
void* calloc(size_t num, size_t size);
Parameters
Parameter | Description |
---|---|
num | Number of elements to allocate. |
size | Size of each element in bytes. (size_t is an unsigned integral type.) |
The function allocates a contiguous block of memory that is zero-initialized. This means every byte in the allocated memory is set to 0. It is useful when you need a clean slate, and note that if a request is made for zero bytes, the behavior is implementation-dependent and the returned pointer should not be dereferenced.
Return Value
On success, calloc()
returns a pointer to the allocated memory block cast to void*
. If the allocation fails, a null pointer is returned.
Examples for calloc()
Example 1: Allocating and Initializing an Integer Array
This example demonstrates how to use calloc()
to allocate memory for an integer array, which is automatically zero-initialized. The output confirms that every element in the array is set to 0.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *arr = (int *)calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Integer array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Output:
Integer array elements: 0 0 0 0 0
Explanation:
In this program, calloc()
is used to allocate memory for an array of 5 integers. Since the allocated memory is zero-initialized, all elements of the array start with the value 0. This makes the array ready for use without the need for further initialization.
Example 2: Allocating and Zero-Initializing an Array of Structures
This example demonstrates how to allocate memory for an array of structures using calloc()
. Zero-initialization guarantees that all numeric fields are set to 0 and any character arrays within the structure are initialized to an empty string, ensuring a known starting state.
Program
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
char name[20];
} Person;
int main() {
int count = 3;
Person *people = (Person *)calloc(count, sizeof(Person));
if (people == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Display the zero-initialized structures
for (int i = 0; i < count; i++) {
printf("Person %d: id = %d, name = \"%s\"\n", i + 1, people[i].id, people[i].name);
}
free(people);
return 0;
}
Output:
Person 1: id = 0, name = ""
Person 2: id = 0, name = ""
Person 3: id = 0, name = ""
Explanation:
Here, calloc()
allocates memory for an array of 3 Person
structures. Each structure is automatically initialized so that the id
field is 0 and the name
field is an empty string. This default state is particularly useful when the structures will be filled with valid data later in the program.