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β thealignmentargument was not a power of two, or was not a multiple ofsizeof(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
- Copyright 2001, John Levon <moz@compsoc.man.ac.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms