Regarding std::align
:
void* align(size_t alignment, size_t size, void*& ptr, size_t& space)
Given a pointer
ptr
to a buffer [...] returns a pointer aligned by the specified alignment [...]. The function modifies the pointer [...]
If align
is successful, then the returned pointer and ptr
are the same.
This SO answer shows passing an offset address that leaves space for a pointer to align
, storing the original pointer to the oversized chunk, and finally calling delete on the original pointer. Is that necessary, or is it safe to simply do this:
size_t space = 64;
void* ptr = malloc(space);
void* ret = std::align(alignment, targetSize, ptr, space);
if (ret == nullptr) { /* failed to align */ }
free(ptr);