void* calloc (size_t num, size_t size);
Allocate and zero-initialize array
Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.
The effective result is the allocation of a zero-initialized memory block of
If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.
- Number of elements to allocate.
- Size of each element.
size_t is an unsigned integral type.
On success, a pointer to the memory block allocated by the function.
The type of this pointer is always
void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a null pointer is returned.
/* calloc example */
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* calloc, exit, free */
int main ()
int * pData;
printf ("Amount of numbers to be entered: ");
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
printf ("Enter number #%d: ",n+1);
printf ("You have entered: ");
for (n=0;n<i;n++) printf ("%d ",pData[n]);
This program simply stores numbers and then prints them out. But the number of items it stores can be adapted each time the program is executed because it allocates the needed memory during runtime.
Only the storage referenced by the returned pointer is modified. No other storage locations are accessed by the call.
If the function reuses the same unit of storage released by a deallocation function (such as free or realloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.
No-throw guarantee: this function never throws exceptions.
- Deallocate memory block (function)
- Allocate memory block (function)
- Reallocate memory block (function)