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);
[[deprecated]] void *valloc(size_t size);
#include <malloc.h>
[[deprecated]] void *memalign(size_t alignment, size_t size);
[[deprecated]] void *pvalloc(size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
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 *)
. This address can later be successfully passed
to free(3). If size
is 0, then the value
placed in *memptr
is either NULL or a unique pointer value.
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.
aligned_alloc() is the same as
memalign(), except for the added restriction that
alignment
must be a power of two.
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 on success. On error, NULL is returned,
and errno
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 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-2008 TC2.
brk(2), getpagesize(2), free(3), malloc(3)