C Reference ยท stdlib.h
C calloc() function
Reference for C calloc(): allocate an array of zeroed elements, how it differs from malloc plus memset, overflow checking, and the pointer and float caveats.
calloc() allocates memory for an array of count elements of size bytes each and sets every byte of the block to zero. It is malloc() with two conveniences: the multiplication count * size is done for you, and the standard library is expected to refuse a product that does not fit in size_t, and the memory comes back cleared.
Zero bytes mean zero for all integer types, and 0.0 for float and double on every platform with IEEE 754 numbers (all common ones). They do not necessarily mean a null pointer on exotic hardware, although they do on every mainstream system.
#include <stdlib.h> void *calloc(size_t count, size_t size);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
void *calloc(size_t count, size_t size);
| Name | Description |
|---|---|
count |
Number of elements. |
size |
Size of one element in bytes (use sizeof *ptr). |
Returns: a pointer to the zeroed block of count * size bytes, or NULL if memory is not available or if the product does not fit in size_t (a quality implementation reports this; a hostile count is still best validated yourself). A request for zero bytes is implementation defined, as for malloc().
Errors / undefined behavior: the same rules as malloc(): access outside the block, use after free() and a second free() are undefined. NULL must be checked.
Usage notes
- Prefer
callocfor arrays, even if you plan to overwrite every element, because its overflow check (when the library implements it) protects against a classic security bug (n * sizeofwrapping around); still validate counts that come from untrusted input. - All-bits-zero is not always the right zero. It is the value 0 for integers and
falseforbool. For floating point it is+0.0on IEEE 754 systems. It is a null pointer on all mainstream platforms, but the C standard does not promise it, so portable code that needs a guaranteedNULLassigns it explicitly. - Speed. For large blocks
callocis often as fast asmalloc, because fresh pages from the operating system are already zero and need no clearing. For small blocks it costs amemset. - No hidden padding guarantees. The padding bytes inside a zeroed struct are zero, but after you assign members the padding may be anything. Do not compare structs with
memcmp. - Growing the block later with
realloc()does not zero the new part. If you need that, clear it yourself withmemset. - Free it normally with
free(). - Initial zero is easy to rely on by accident: a block that only works because it started zero hides bugs in code that later switches to
malloc.
Related
malloc,realloc,free,memset- Lessons: Dynamic memory, Dynamic arrays