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 errno may 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> πŸ“š


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