C calloc()
In this page:
data_type *pointer_name = (data_type *) calloc(count, sizeof(data_type)); // all bytes set to zero
What is calloc()?
The zero-initialization has a real cost -- calloc() has to write zeros across the entire block, which is a small amount of extra work compared to malloc(), but it's usually worth it for the safety guarantee of starting from a known state.
Example: What is calloc()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
int *arr = calloc(3, sizeof(int));
// Print formatted text to the screen
printf("%d %d %d", arr[0], arr[1], arr[2]);
// Release the heap memory so it can be reused
free(arr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
calloc() vs malloc()
In practice, calloc(n, size) is roughly equivalent to malloc(n * size) followed by memset() to zero it out, but calloc() also protects against integer overflow in that multiplication in a way manual code often doesn't.
Example: calloc() vs malloc()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *a = malloc(3 * sizeof(int));
int *b = calloc(3, sizeof(int));
// Print formatted text to the screen
printf("%d", b[0]);
// Release the heap memory so it can be reused
free(a);
// Release the heap memory so it can be reused
free(b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Allocating Arrays
This convenience is especially valuable for arrays of counters or accumulators, where starting from zero is exactly the behavior you want rather than something you'd otherwise have to set up manually in a loop after allocating.
Example: Allocating Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
int *counters = calloc(5, sizeof(int));
counters[2]++;
// Print formatted text to the screen
printf("%d", counters[2]);
// Release the heap memory so it can be reused
free(counters);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Allocating Structures
For a struct with pointer members, zero-initialization means those pointers start as NULL rather than garbage addresses, which makes subsequent NULL-checks meaningful before those fields have been properly assigned.
Example: Allocating Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Node
struct Node { int value; struct Node *next; };
// Program execution starts in main()
int main() {
struct Node *n = calloc(1, sizeof(struct Node));
// Print formatted text to the screen
printf("%d", n->next == NULL);
// Release the heap memory so it can be reused
free(n);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Common Mistakes with calloc()
Just like with malloc(), always assign calloc()'s return value to a temporary variable first and check it before overwriting your original pointer, so you don't lose your only reference to previously allocated memory if this call fails.
Example: Common Mistakes with calloc()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
int *ptr = calloc(3, sizeof(int));
int *temp = calloc(3, sizeof(int));
// Check whether temp != NULL
if (temp != NULL) {
// Release the heap memory so it can be reused
free(ptr);
ptr = temp;
}
// Print formatted text to the screen
printf("%d", ptr[0]);
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Not checking whether
callocreturnedNULL. - Assuming
mallocalso zeroes memory, when onlycallocdoes. - Forgetting to
freememory allocated withcalloc.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: