The calloc function performs dynamic memory allocation in C, and is part of the standard library.
The standard C library declares the function calloc() as follows:
void *calloc(size_t elements, size_t sz);
calloc() allocates space for an array of elements, each of which occupies sz bytes of storage. The space of each element is initialized to binary zeros. In other words, calloc() is similar to malloc(), except that it handles arrays of objects rather than a single chunk of storage and that it initializes the storage allocated. The following example allocates an array of 100 int's using calloc():
int * p = (int*) calloc (100, sizeof(int));
NAME
calloc - a memory allocator
SYNOPSIS
#include <stdlib.h> void *calloc(size_t nelem, size_t elsize);
DESCRIPTION
The
calloc()
function allocates memory for an array of nelem elements of elsize bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nelem or elsize is 0, thencalloc()
returns either NULL, or a unique pointer value that can later be successfully passed tofree()
.
RETURN VALUE
The
calloc()
function returns a pointer to the allocated memory that is suitably aligned for any kind of variable. On error, it returns NULL. NULL may also be returned by a successful call with nelem or elsize equal to zero.
Wikipedia
References
- calloc - cppreference.com
- calloc - The Open Group Base Specifications Issue 7
- Ubuntu Manpage: malloc, free, calloc, realloc - Allocate and free dynamic memory
- Memory Allocation Routines - C Run-Time Library Reference (Visual Studio 2012)
- calloc - C Run-Time Library Reference (Visual Studio 2012)