CALLOC(3P)
π CALLOC(3P) β the POSIX Programmer's Manual page, bestie edition π
okay so before we even get into it, hi, hello, welcome, this is the calloc page from the POSIX Programmer's Manual and I am so excited to walk you through it. grab a snack. π
πΈ PROLOG πΈ
so like, real talk first. This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
bestie that's literally the whole disclaimer. it's giving "don't come for me if Linux does it different." π
β¨ NAME β¨
calloc β a memory allocator π
that's it. that's the whole name section. she's short, she's iconic, she allocates memory.
π SYNOPSIS π
#include <stdlib.h>
void *calloc(size_t nelem, size_t elsize);
two args, one pointer back, no cap. π
π¦ DESCRIPTION π¦
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1β2017 defers to the ISO C standard.
okay now the actual tea β:
The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.
so yes, it zeroes everything out for you. that's her whole thing. the moment. β¨
The order and contiguity of storage allocated by successive calls to calloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned shall point to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behavior is implementation-defined: either a null pointer shall be returned, or the behavior shall be as if the size were some non-zero value, except that the behavior is undefined if the returned pointer is used to access an object.
β οΈ okay the 0-size thing? that's a vibe check you do NOT want to fail. implementation-defined means "whatever your libc feels like that day," and if you then use that pointer to access an object, that's undefined behavior. do not. π«Ά
π RETURN VALUE π
Upon successful completion with both nelem and elsize non-zero, calloc() shall return a pointer to the allocated space. If either nelem or elsize is 0, then either:
- A null pointer shall be returned and
errnomay be set to an implementation-defined value, or - A pointer to the allocated space shall be returned. The application shall ensure that the pointer is not used to access an object.
Otherwise, it shall return a null pointer and set errno to indicate the error.
so like, two paths for the zero case, both valid, pick your fighter. but if it fails fails? null pointer + errno gets set, and that's your signal to go touch grass and figure out what went wrong. π
π₯ ERRORS π₯
The calloc() function shall fail if:
ENOMEMβ Insufficient memory is available.
that's the one. ran out of memory, she said no. π
The following sections are informative.
π EXAMPLES π
None.
she said no examples, we move. π
β APPLICATION USAGE β
There is now no requirement for the implementation to support the inclusion of <malloc.h>.
so if you were out here #include <malloc.h>-ing like it's 2001... the standard is not requiring that anymore. use <stdlib.h> like the synopsis told you. periodt. π
π« RATIONALE π«
None.
π§ FUTURE DIRECTIONS π§
None.
π SEE ALSO π
free()malloc()realloc()
The Base Definitions volume of POSIX.1β2017, <stdlib.h>
the whole squad. they're a package deal fr fr. π«Ά
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the page!! mwah π this was IEEE/The Open Group, 2017, signing off on CALLOC(3P) β go forth and allocate responsibly, bestie. no leaks. no dangling pointers. main character energy only. πβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms