aligned_alloc(3)
π aligned_alloc(3) π
okay so bestie, this is a page from the Library Functions Manual β yes we're deep in the Linux man-pages 6.20 zone β and today's main character is aligned_alloc, a libc girlie whose whole thing is giving you memory that's lined up just right. β¨
πΈ NAME πΈ
aligned_alloc β allocate aligned memory π
π LIBRARY π
Standard C library β that's libc, -lc when you're linking. iykyk π
β¨ SYNOPSIS β¨
#include <stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
π§ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
aligned_alloc():_ISOC11_SOURCE
π¦ DESCRIPTION π¦
aligned_alloc() grabs size bytes and hands you back a pointer to that allocated memory. The tea? β The memory address is literally going to be a multiple of alignment, and alignment has to be a power of two. no cap. And then later you can pass that pointer to free(3) and it will work just fine, bestie. π«Ά
β οΈ heads up: the memory is not zeroed. not even a little. don't assume it's clean. π
π RETURN VALUE π
Slay β on success, aligned_alloc() returns a pointer to the allocated memory. On error it returns NULL, and errno gets set so you know what went wrong. π
π ERRORS π
EINVALβ bestie. thealignmentargument was not a power of two. girl. πENOMEMβ out of memory. it's giving exhausted. π
β ATTRIBUTES β
for an explanation of the terms used in this section, see attributes(7).
- Interface:
aligned_alloc() - Attribute: Thread safety
- Value:
MT-Safe
so yes, she's thread safe. iconic behavior honestly. π
π STANDARDS π
C23, POSIX.1-2024.
πΈ HISTORY πΈ
glibc 2.16. C11, POSIX.1-2024.
π C11
okay so in C11, the spec for this function had several issues. like, actual problems. π
sizehad to be a multiple ofalignment. otherwise the behavior was undefined.- If
alignmentwas not a power of two, the behavior was undefined.
DR460 reported both of those UB cases as unnecessarily dangerous, and fixed them with a Technical Corrigendum that turned them into errors. periodt. β¨
N2072 reported that the requirement that size is a multiple of alignment is superfluous, and removed it with a Technical Corrigendum.
C17 incorporates both technical corrigenda. The API has been stable ever since C17. π
glibc initially implemented it as silently aligning as stdc_bit_ceil(alignment) instead of alignment. Since glibc 2.38, it implements the C17 specification.
Some implementations, such as FreeBSD/jemalloc, implement the C17 specification, even though their documentation claims having undefined behavior. messy but ok. π
Some implementations, such as OpenBSD, implement C11 amended with DR460, even though their documentation claims having undefined behavior. same vibe honestly.
No known implementations have exploited the undefined behavior in a more dangerous way. This function should be safe to use. π«Άβ¨
π NOTES π
On many systems there are alignment restrictions β for example, on buffers used for direct block device I/O. POSIX specifies the pathconf(path,_PC_REC_XFER_ALIGN) call that tells you what alignment is needed. And now? you can use aligned_alloc() to satisfy that requirement. ate. π
The glibc malloc(3) always returns 8-byte aligned memory addresses, so this function is needed only if you require larger alignment values. π₯
π¦ SEE ALSO π¦
brk(2), getpagesize(2), free(3), malloc(3)
and that's the whole page bestie!! π go forth and align ur memory responsibly. this was Linux man-pages 6.20, dated 2025-12-25 β mwah, happy reading, ily, bye!! πβ¨π«Άπ
π¦ where this page comes from
- 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 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms