MALLOC(3P)
π MALLOC(3P) π
ok bestie, buckle up π this one's straight outta the POSIX Programmer's Manual, and it's literally the blueprint for getting memory in C. Yes, that malloc. The one you've been calling since your very first Segmentation fault (core dumped) π«Ά
β¨ PROLOG β¨
ok so real talk first π this manual page is part of the POSIX Programmer's Manual. But heads up β the Linux implementation of this interface may differ (like, consult the corresponding Linux manual page for the deets on Linux behavior, okay?), or honestly the interface may just not be implemented on Linux at all. Not every system shows up to the party π
π NAME π
malloc β a memory allocator. that's her. that's the girl π
π¦ SYNOPSIS π¦
#include <stdlib.h>
void *malloc(size_t size);
one include, one function call. so simple, so powerful, so dangerous π₯
π DESCRIPTION π
ok so first thing: everything described on this reference page is aligned with the ISO C standard. And if there's any conflict between the requirements described here and the ISO C standard? That is unintentional, bestie π. This volume of POSIX.1β2017 defers to the ISO C standard. Periodt.
malloc() shall allocate unused space for an object whose size in bytes is specified by size, and whose value is unspecified. So you get the bytes, but the bytes are givingβ¦ mystery π΅οΈβοΈ
Now β the order and contiguity of storage allocated by successive calls to malloc() is unspecified. Don't go assuming your second call lands right after your first, okay? no cap.
If the allocation succeeds, the pointer you get back shall be suitably aligned β aligned enough that you can assign it to a pointer to any type of object and then use it to access such an object in the space that was allocated (until that space is explicitly freed or reallocated, obviously π ).
Each such allocation shall yield a pointer to an object that is disjoint from any other object. like, separate. no overlap. we don't do that here.
The pointer returned points to the start of the allocated space β that's the lowest byte address, fyi.
If the space cannot be allocated, a null pointer shall be returned. π
And if the size of the space requested is 0? whew okay, 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. So likeβ¦ don't. do NOT do that π
π«Ά RETURN VALUE π«Ά
Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space. easy β¨
If size is 0, it's one of two things, and honestly pick your fighter:
- 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 β and then the application shall ensure that the pointer is not used to access an object. like bestie, do not touch it, it is decorative π
Otherwise, it shall return a null pointer and set errno to indicate the error.
π ERRORS π
malloc() shall fail if:
ENOMEMβ Insufficient storage space is available. aka there's just literally not enough room, babe π
The following sections are informative. (like, they're extra, they're vibes, they're not the requirements π )
π EXAMPLES π
None. π yeah. nothing here. we move.
πΈ APPLICATION USAGE πΈ
None. π
π§ RATIONALE π§
None. π¦
β FUTURE DIRECTIONS β
None. π
π SEE ALSO π
calloc()free()getrlimit()posix_memalign()realloc()
And the Base Definitions volume of POSIX.1β2017, <stdlib.h> π
π COPYRIGHT π
ok so legally I have to say all of this and I'm gonna say it exactly, bestie π«Ά
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 whole page!! π brought to you by IEEE/The Open Group in 2017, signing off from this little corner of MALLOC(3P). now go free your memory, bestie β or don't, I'm a manual page, not a cop π«Άβ¨
π¦ 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