Data Structures · Dynamic Memory Allocation
calloc and Zero Initialization
Overview
calloc is a dynamic memory allocation function that behaves like malloc but takes its size request in two parts (number of elements and size of each element) and zero-initializes the entire allocated block before returning it.
It matters because it guarantees clean, zeroed memory and contiguous allocation, removing the garbage-value problem of malloc.
Key concepts
calloc(spoken "C-lock" / "CA-lock"): a dynamic memory allocation function exactly similar tomalloc, but the size of the requested block is specified in a different way — the number of elements and the size of each element are given separately instead of the total number of bytes.- Zero initialization:
callocreturns the block already initialized to zero over the entire allocated space, before the pointer is given to you.mallocreturns a block containing garbage values if you do not initialize it explicitly. - Contiguity guarantee: both
mallocandcallocalways give space in a contiguous manner. If the requested space is not available as one contiguous block, the function will not give you the space. - Typecasting: with
calloc, exactly as withmalloc, you must convert the returned pointer to the type you want to store it in — anintpointer or acharpointer, depending on the data you plan to store.
How it works
- Specify the request in two parts: how many elements you want to store, and the size of each element.
callocmultiplies the two values to compute the total bytes to allocate.calloccreates the space, initializes all of that space to zero, and then returns a pointer to the starting address of the space.- The returned pointer is the address of the starting byte of the allocated space.
- The space is always contiguous; scattered free blocks are never combined to satisfy the request.
- Typecast the returned pointer to the appropriate pointer type (
int *,char *, etc.).
Formulas
- Total bytes requested = (number of elements) × (size of each element)
- For
calloc(n, s): = number of elements, = size of each element in bytes; total bytes .
Worked examples
Example 1: calloc(2, 2)
- Number of elements = 2
- Size of each element = 2 bytes
- Total bytes = bytes
Example 2: calloc(10, 2)
- Number of elements = 10
- Size of each element = 2 bytes
- Total bytes = bytes
Example 3 (contiguity): Request = 20 bytes. Free memory available = 10 bytes in one place + 10 bytes in another place.
- Result: the request is not satisfied, because the two 10-byte blocks are not one contiguous block.
Comparison
| Feature | malloc | calloc |
|---|---|---|
| Way of specifying size | Total size in bytes given directly | Number of elements and size of each element given separately |
| Initialization | Garbage values present if not initialized explicitly | Entire allocated space initialized to zero before returning |
| Return value | Pointer to starting byte of the block | Pointer to starting byte of the block |
| Contiguity | Always contiguous; no block if contiguous space unavailable | Always contiguous; no block if contiguous space unavailable |
| Typecasting | Required | Required |
Revision summary
calloctakes two arguments — number of elements and size of each element; total bytes = product of the two.calloczero-initializes the whole block;mallocleaves garbage values.- Both
mallocandcallocreturn the address of the starting byte of the allocated space. - Both always allocate contiguous memory; scattered free blocks are never combined.
- The pointer returned by
callocmust be typecast to the appropriate pointer type (int *,char *, etc.). - A request fails if the needed bytes are not available as a single contiguous block, even if the total free bytes elsewhere are sufficient.
Previous-year exam insights
- realloc(ptr, new_size) resizes a previously allocated block: it may extend it in place or move it to a new location, preserving the existing contents up to the smaller of the old and new sizes, and returns a pointer to the (possibly new) block.
Asked inUGC NET 2013 - free(ptr) releases a block previously returned by malloc/calloc/realloc back to the heap; it does not change the pointer variable itself, so the pointer becomes dangling and must not be dereferenced or freed twice.
Asked inUGC NET 2013 - malloc(size) takes a single argument, the total number of bytes, and returns void* (NULL on failure); calloc(n, size) is the array-oriented allocator, so calloc is the one described as allocating space for an array of n elements.
Asked inUGC NET 2013 - All four functions malloc, calloc, realloc and free are declared in <stdlib.h>; malloc/calloc/realloc return NULL if the request cannot be satisfied, so the returned pointer should be checked before use.
- In C a void* returned by malloc/calloc converts implicitly, so typecasting is optional (not required); in C++ an explicit cast is mandatory.
- calloc sets every allocated byte to zero, so integer elements read as 0 (on common platforms pointers read as null and floating-point values as 0.0, though the C standard guarantees this only for integer types); malloc leaves indeterminate contents that must be initialized before reading.