NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |SEE ALSO |COLOPHON | |
posix_memalign(3) Library Functions Manualposix_memalign(3)posix_memalign, aligned_alloc, memalign, valloc, pvalloc - allocate aligned memory
Standard C library (libc,-lc)
#include <stdlib.h>int posix_memalign(void **memptr, size_talignment, size_tsize);void *aligned_alloc(size_talignment, size_tsize);[[deprecated]] void *valloc(size_tsize);#include <malloc.h>[[deprecated]] void *memalign(size_talignment, size_tsize);[[deprecated]] void *pvalloc(size_tsize); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):posix_memalign(): _POSIX_C_SOURCE >= 200112Laligned_alloc(): _ISOC11_SOURCEvalloc(): Since glibc 2.12: (_XOPEN_SOURCE >= 500) && !(_POSIX_C_SOURCE >= 200112L) || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE Before glibc 2.12: _BSD_SOURCE || _XOPEN_SOURCE >= 500
posix_memalign() allocatessize bytes and places the address of the allocated memory in*memptr. The address of the allocated memory will be a multiple ofalignment, which must be a power of two and a multiple ofsizeof(void *). This address can later be successfully passed tofree(3). Ifsize is 0, then the value placed in*memptr is either NULL or a unique pointer value. The obsolete functionmemalign() allocatessize bytes and returns a pointer to the allocated memory. The memory address will be a multiple ofalignment, which must be a power of two.aligned_alloc() is the same asmemalign(), except for the added restriction thatalignment must be a power of two. The obsolete functionvalloc() allocatessize bytes and returns a pointer to the allocated memory. The memory address will be a multiple of the page size. It is equivalent tomemalign(sysconf(_SC_PAGESIZE),size). The obsolete functionpvalloc() is similar tovalloc(), but rounds the size of the allocation up to the next multiple of the system page size. For all of these functions, the memory is not zeroed.
aligned_alloc(),memalign(),valloc(), andpvalloc() return a pointer to the allocated memory on success. On error, NULL is returned, anderrno is set to indicate the error.posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. The value oferrno is not set. On Linux (and other systems),posix_memalign() does not modifymemptr on failure. A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
EINVALThealignment argument was not a power of two, or was not a multiple ofsizeof(void *).ENOMEMOut of memory.
For an explanation of the terms used in this section, seeattributes(7). ┌───────────────────────────────┬───────────────┬────────────────┐ │Interface│Attribute│Value│ ├───────────────────────────────┼───────────────┼────────────────┤ │aligned_alloc(),memalign(), │ Thread safety │ MT-Safe │ │posix_memalign() │ │ │ ├───────────────────────────────┼───────────────┼────────────────┤ │valloc(),pvalloc() │ Thread safety │ MT-Unsafe init │ └───────────────────────────────┴───────────────┴────────────────┘
aligned_alloc() C11.posix_memalign() POSIX.1-2008.memalign()valloc() None.pvalloc() GNU.
aligned_alloc() glibc 2.16. C11.posix_memalign() glibc 2.1.91. POSIX.1d, POSIX.1-2001.memalign() glibc 2.0. SunOS 4.1.3.valloc() glibc 2.0. 3.0BSD. Documented as obsolete in 4.3BSD, and as legacy in SUSv2.pvalloc() glibc 2.0.Headers Everybody agrees thatposix_memalign() is declared in<stdlib.h>. On some systemsmemalign() is declared in<stdlib.h> instead of<malloc.h>. According to SUSv2,valloc() is declared in<stdlib.h>. glibc declares it in<malloc.h>, and also in<stdlib.h> if suitable feature test macros are defined (see above).
On many systems there are alignment restrictions, for example, on buffers used for direct block device I/O. POSIX specifies thepathconf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed. Now one can useposix_memalign() to satisfy this requirement.posix_memalign() verifies thatalignment matches the requirements detailed above.memalign() may not check that thealignment argument is correct. POSIX requires that memory obtained fromposix_memalign() can be freed usingfree(3). Some systems provide no way to reclaim memory allocated withmemalign() orvalloc() (because one can pass tofree(3) only a pointer obtained frommalloc(3), while, for example,memalign() would callmalloc(3) and then align the obtained value). The glibc implementation allows memory obtained from any of these functions to be reclaimed withfree(3). The glibcmalloc(3) always returns 8-byte aligned memory addresses, so these functions are needed only if you require larger alignment values.
brk(2),getpagesize(2),free(3),malloc(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17posix_memalign(3)Pages that refer to this page:io_uring_register(2), io_uring_register_buf_ring(3), malloc(3), malloc_hook(3), mallopt(3), mtrace(3), pthread_attr_setstack(3)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |