Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

calloc - Reserve memory space

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

void *calloc(size_t n, size_t elsize);

calloc provides contiguous memory space at execution time for an array with n elements,
where each element requires elsize bytes. calloc initializes each element of the new array
with binary zeros.

calloc is part of a C-specific memory management package which internally manages
requested and released memory areas. Wherever possible, new requests are met first from
areas already being managed and only then by the operating system (cf. garbcoll
function).

Return val.

Pointer to the new memory space

if sufficient memory space is present.

NULL pointer

if memory space does not suffice for the request.

Notes

The new data area begins on a doubleword boundary.

To ensure that you are requesting the correct size for an array element, you should use the
sizeof operator for the calculation of elsize.

A serious disruption in working memory may be expected if the length of the memory area
provided is exceeded when writing.

If n or elsize has the value 0, calloc returns an unambiguous address which can also be
transferred to free.

Example

The following program fragment requests memory space for 20 array elements of type long
integer.

#include <stdlib.h>
long *long_array;
    .
    .
long_array = (long *)calloc(20, sizeof(long));

See also

malloc, realloc, free, garbcoll