There exist several aligned versions of the venerable malloc()
, e.g.:
#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);
#include <malloc.h>
void *memalign(size_t alignment, size_t size);
(originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc()
which supports alignment. Has it really never been implemented? It seems pretty trivial to combine the functionality of non-aligned realloc()
with the search for an aligned chunk of memory in the aligned malloc()
variants.
Related:
malloc()
saves? I highly doubt it... anyway, if you can flesh this out some more, preferable with a link to some of that discussion, please post an answer. – Undersignedrealloc()
with the search for an aligned chunk of memory in the alignedmalloc()
variants. And that would be a very simple reason for not implementing it. It's "pretty trivial". – Alguireposix_memalign()
,memcpy()
,free()
, and a bit of error checking pretty trivial even from the outside of the allocator. – Alguiremmap()
. Even though they aren't standard C or even POSIX, they are supported by most systems (excluding Windows, of course). (For large amounts of memory, allocating a copy at the same time, for @AndrewHenle's workaround, may not be possible at all. Consider e.g. a 2GB+ object on a 32-bit system.) – Banyan