C Notes
Complete guide to calloc() in C programming — syntax, zero-initialization, difference from malloc, practical examples with output, when to use calloc vs malloc, and common interview questions.
While malloc() gives you a raw block of uninitialized memory, calloc() does something more thoughtful — it allocates memory and initializes every byte to zero. The name stands for contiguous allocation, and it's especially useful when you need a clean slate of memory.
Syntax of calloc()
void *calloc(size_t num, size_t size);| Parameter | Description |
|---|---|
num | Number of elements to allocate |
size | Size of each element in bytes |
| Return value | Pointer to zero-initialized memory, or NULL on failure |
Notice the difference from malloc: calloc takes two arguments. Instead of calculating the total bytes yourself, you specify how many items and how big each item is.
How calloc() Works
Basic calloc() Example
Values after calloc (all zeros): arr[0] = 0 arr[1] = 0 arr[2] = 0 arr[3] = 0 arr[4] = 0 After assignment: arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16
calloc() vs malloc() – Key Differences
| Feature | malloc() | calloc() |
|---|---|---|
| Syntax | malloc(size) | calloc(num, size) |
| Arguments | 1 (total bytes) | 2 (count, element size) |
| Initialization | Garbage values | Zero-initialized |
| Speed | Slightly faster | Slightly slower (due to zeroing) |
| Overflow check | No | Yes (checks num × size overflow) |
| Use case | When you'll overwrite immediately | When zero-init matters |
Demonstrating the Difference
malloc values (garbage): m_arr[0] = 1852730451 m_arr[1] = 7565156 m_arr[2] = 0 m_arr[3] = 1769234802 m_arr[4] = 2036691 calloc values (zeros): c_arr[0] = 0 c_arr[1] = 0 c_arr[2] = 0 c_arr[3] = 0 c_arr[4] = 0
When to Use calloc() Over malloc()
Use calloc() when:
- Counters and accumulators — Starting from zero makes logical sense
- Lookup tables — Zero means "not set" or "empty"
- Graph adjacency matrices — Zero means "no edge"
- Boolean flag arrays — Zero means false
- Security-sensitive code — Prevents information leakage from uninitialized memory
Practical Example: Frequency Counter
Letter frequencies: 'a' = 1 'c' = 1 'd' = 1 'e' = 1 'g' = 2 'h' = 1 'i' = 2 'l' = 3 'm' = 2 'n' = 2 'o' = 3 'p' = 1 'r' = 3 'w' = 1
calloc() for 2D Arrays
Matrix: 1 0 0 0 0 5 0 0 0 0 0 9
Overflow Safety of calloc()
One subtle advantage of calloc(num, size) over malloc(num * size) is overflow detection:
// DANGEROUS: if num is huge, num * sizeof(int) can overflow silently
size_t num = 1000000000000;
int *p = (int *)malloc(num * sizeof(int)); // Overflow → allocates tiny block!
// SAFER: calloc checks for multiplication overflow internally
int *q = (int *)calloc(num, sizeof(int)); // Returns NULL on overflowSimulating calloc() with malloc() + memset()
arr[0] = 0 arr[1] = 0 arr[2] = 0 arr[3] = 0 arr[4] = 0
Interview Questions
Q1: What is the main difference between malloc() and calloc()?
malloc() takes one argument (total bytes) and returns uninitialized memory. calloc() takes two arguments (count and element size) and returns zero-initialized memory. calloc() is slightly slower because it must write zeros to the entire block.
Q2: Does calloc() guarantee zero-initialization for all types?
calloc() sets all bytes to zero. For integers and pointers, this is equivalent to the value 0. However, for floating-point numbers, all-bits-zero is 0.0 on IEEE 754 systems (which is virtually all modern systems), but this is technically implementation-defined.
Q3: When would you prefer malloc() over calloc()?
When you plan to immediately overwrite every allocated byte (e.g., reading file contents into a buffer). The zero-initialization of calloc() would be wasted work in that case, so malloc() is more efficient.
Q4: Can calloc() fail?
Yes. Like malloc(), calloc() returns NULL when the system cannot satisfy the memory request. Additionally, calloc() may fail if the multiplication of num * size would overflow.
Summary
calloc()allocates memory for an array of elements and initializes all bytes to zero- Syntax:
calloc(num_elements, size_per_element) - Safer than
malloc(n * size)because it checks for integer overflow - Use calloc() when zero-initialization is meaningful (counters, flags, lookup tables)
- Use malloc() when you'll overwrite the data immediately
- Always check for NULL and always free the memory when done
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for calloc() in C – Contiguous Memory Allocation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, dynamic, memory, allocation, calloc, calloc() in c – contiguous memory allocation
Related C Programming Topics