posix_memalign, aligned_alloc, memalign, valloc, pvalloc - allocate aligned memory
#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);
void *valloc(size_t size);
#include <malloc.h>
void *memalign(size_t alignment, size_t size);
void *pvalloc(size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
The function posix_memalign() allocates size
bytes and places the address of the allocated memory in *memptr
. The address of the allocated memory will be a multiple of alignment
, which must be a power of two and a multiple of sizeof(void *)
. If size
is 0, then the value placed in *memptr
is either NULL, or a unique pointer value that can later be successfully passed to free(3).
The obsolete function memalign() allocates size
bytes and returns a pointer to the allocated memory. The memory address will be a multiple of alignment
, which must be a power of two.
The function aligned_alloc() is the same as memalign(), except for the added restriction that size
should be a multiple of alignment
.
The obsolete function valloc() allocates size
bytes and returns a pointer to the allocated memory. The memory address will be a multiple of the page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size)
.
The obsolete function pvalloc() is similar to valloc(), 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(), and pvalloc() return a pointer to the allocated memory, or NULL if the request fails.
posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. The value of errno
is not set. On Linux (and other systems), posix_memalign() does not modify memptr
on failure. A requirement standardizing this behavior was added in POSIX.1-2016.
brk(2), getpagesize(2), free(3), malloc(3)