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. the alignment argument 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. πŸ™ˆ

  • size had to be a multiple of alignment. otherwise the behavior was undefined.
  • If alignment was 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