The calloc function helps you get memory for multiple items and sets all that memory to zero. This makes sure the memory is clean and ready to use.
calloc function
void *calloc(size_t num_items, size_t size_of_item);num_items is how many items you want.
size_of_item is the size in bytes of each item.
int *arr = (int *)calloc(5, sizeof(int));
char *buffer = (char *)calloc(100, sizeof(char));
Point structures, all bytes zeroed.struct Point *points = (struct Point *)calloc(10, sizeof(struct Point));This program creates an array of 4 integers using calloc. It then prints the values, which are all zero because calloc clears the memory. Finally, it frees the memory.
#include <stdio.h> #include <stdlib.h> int main() { int n = 4; int *arr = (int *)calloc(n, sizeof(int)); if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } printf("Array values after calloc:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); free(arr); return 0; }
Always check if calloc returns NULL, which means memory allocation failed.
Remember to use free() to release the memory when done to avoid memory leaks.
calloc differs from malloc because it clears the memory to zero.
calloc allocates memory for multiple items and sets all bytes to zero.
Use it when you want clean, zeroed memory for arrays or structures.
Always check for NULL and free the memory after use.
Practice
calloc function do in C?Solution
Step 1: Understand
callocpurposecallocallocates memory for an array of elements and initializes all bytes to zero.Step 2: Compare with other functions
mallocallocates memory but does not initialize it,freereleases memory, andmemcpycopies memory.Final Answer:
Allocates memory for multiple elements and initializes all bytes to zero -> Option DQuick Check:
calloc= zeroed memory allocation [OK]
- Confusing calloc with malloc (uninitialized memory)
- Thinking calloc frees memory
- Assuming calloc copies data
calloc?Solution
Step 1: Recall
callocparameterscalloctakes two arguments: number of elements and size of each element.Step 2: Match correct order and types
int *arr = calloc(10, sizeof(int)); correctly usescalloc(10, sizeof(int)). Other options have wrong order or missing arguments.Final Answer:
int *arr = calloc(10, sizeof(int)); -> Option CQuick Check:
calloc(count, size) = correct syntax [OK]
- Swapping the two arguments
- Passing only one argument
- Multiplying inside calloc instead of separate arguments
int *ptr = calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);Solution
Step 1: Understand
callocinitializationcallocsets all allocated bytes to zero, so all integers start as 0.Step 2: Analyze the loop output
The loop prints each element, all zero, so output is "0 0 0 0 0 ".Final Answer:
0 0 0 0 0 -> Option BQuick Check:
calloc zeroes memory = zeros printed [OK]
- Expecting garbage values like malloc
- Forgetting to free memory (not causing output error here)
- Assuming uninitialized values
int *arr = calloc(5, sizeof(int)); arr[5] = 10; free(arr);
Solution
Step 1: Check array indexing
Array allocated has indices 0 to 4. Accessing arr[5] is out-of-bounds.Step 2: Confirm other parts
callocsyntax is correct andfreeis called properly.Final Answer:
Accessing out-of-bounds index -> Option AQuick Check:
Index 5 invalid for size 5 array [OK]
- Using index equal to size (off-by-one error)
- Forgetting to free memory
- Misunderstanding calloc parameters
calloc. Which code snippet correctly does this?Solution
Step 1: Understand 2D array allocation
int (*arr)[4] = calloc(3, sizeof(*arr)); allocates 3 rows of arrays each with 4 ints, using pointer to array syntax.Step 2: Check other options
int **arr = calloc(3, sizeof(int*)); arr[0] = calloc(4, sizeof(int)); only allocates first row's columns, not all rows. int *arr = calloc(3 * 4, sizeof(int)); allocates flat 1D array, not 2D. int arr[3][4] = calloc(3, 4); is invalid syntax.Final Answer:
int (*arr)[4] = calloc(3, sizeof(*arr)); -> Option AQuick Check:
Pointer to array + calloc rows = correct 2D allocation [OK]
- Allocating only one row's columns
- Using invalid syntax for arrays
- Allocating flat 1D array instead of 2D
Ready to prove it?
Pass the test and this pattern moves unknown
