posix_memalign(3)

πŸ’– posix_memalign(3) β€” Library Functions Manual πŸ’–

okay bestie, welcome to the posix_memalign page, a certified classic from the Library Functions Manual. yes this is the alignment girlie you've heard about. let's get into it ✨

✨ NAME ✨

posix_memalign β€” allocate aligned memory

🫢 LIBRARY 🫢

Standard C library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <stdlib.h>

int posix_memalign(void **memptr, size_t alignment, size_t size);

πŸŽ€ Feature Test Macro Requirements for glibc (peep feature_test_macros(7)) πŸŽ€

  • posix_memalign():
    • _POSIX_C_SOURCE >= 200112L

πŸ’… DESCRIPTION πŸ’…

so posix_memalign() allocates size bytes and drops the address of that freshly-allocated memory into *memptr. and the address it gives you? it's gonna be a multiple of alignment, which β€” and this is important, no cap β€” must be a power of two and a multiple of sizeof(void *). that same address can later be successfully passed to free(3). iconic, honestly.

if size is 0, then the value placed in *memptr is either NULL or a unique pointer value. either way it's valid, so don't stress πŸ’–

heads up bestie: the memory is not zeroed. it's giving mystery box energy. if you wanted clean memory you're gonna have to do that yourself.

also, the way that aligned_alloc(3) is right there being a standard function with the same functionality and a more ergonomic prototype… yeah. use that instead. the manpage literally said it, not me πŸ™ˆ

⭐ RETURN VALUE ⭐

posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. and the plot twist: the value of errno is not set. you read that right. do not go checking errno for this one, she doesn't play that game.

πŸ“ ERRORS πŸ“

  • EINVAL β€” the alignment argument was not a power of two, or was not a multiple of sizeof(void *). basically you gave her a number she cannot work with πŸ’€
  • ENOMEM β€” out of memory. the machine said nope.

πŸ‘‘ ATTRIBUTES πŸ‘‘

for an explanation of the terms used in this section, see attributes(7).

  • posix_memalign() β€” Thread safety: MT-Safe
MT-Safe!! queen behavior, she's safe to call from multiple threads 🫢

🌈 STANDARDS 🌈

POSIX.1-2024.

πŸ¦‹ HISTORY πŸ¦‹

glibc 2.1.91. Also POSIX.1d, POSIX.1-2001. she's been around, she's got lore πŸ’œ

⭐ CAVEATS ⭐

on Linux (and other systems), posix_memalign() does not modify memptr on failure. so like, if it flops, your pointer is untouched β€” don't go assuming it got set to garbage or NULL. a requirement standardizing this behavior was added in POSIX.1-2008 TC2. iykyk 😌

πŸ’– SEE ALSO πŸ’–


and that's the whole page, bestie!! πŸŽ€ this one came to you straight from Linux man-pages 6.20, dated 2026-02-08, and it's officially wrapped. now go use aligned_alloc(3) like the manpage told you to. mwah πŸ’‹βœ¨

πŸ“¦ 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