← Back to C Course | Chapter 9: Memory Management | Lesson 4 of 8

C calloc()

calloc also asks for memory while the program runs, but it sets everything to zero first, like handing you a freshly wiped whiteboard. It is handy for arrays.
Syntax
c
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()?

c
// 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;
}

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()

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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()

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not checking whether calloc returned NULL.
  2. Assuming malloc also zeroes memory, when only calloc does.
  3. Forgetting to free memory allocated with calloc.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.