std::align (3) - Linux Manuals
std::align: std::align
NAME
Synopsis
Defined in header <memory>
void* align( std::size_t alignment,
std::size_t size, (since C++11)
void*& ptr,
std::size_t& space );
Given a pointer ptr to a buffer of size space, returns a pointer aligned by the specified alignment for size number of bytes and decreases space argument by the number of bytes used for alignment. The first aligned address is returned.
The function modifies the pointer only if it would be possible to fit the wanted number of bytes aligned by the given alignment into the buffer. If the buffer is too small, the function does nothing and returns nullptr.
The behavior is undefined if alignment is not a power of two.
Parameters
alignment - the desired alignment
size - the size of the storage to be aligned
ptr - pointer to contiguous storage of at least space bytes
space - the size of the buffer in which to operate
Return value
The adjusted value of ptr, or null pointer value if the space provided is too small.
Example
demonstrates the use of std::align to place objects of different type in memory
// Run this code
Possible output:
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2377 C++11 alignment required to be a fundamental or supported extended alignment value only need to be a power of two
See also
alignof_operator queries alignment requirements of a type (since C++11)
alignas_specifier specifies that the storage for the variable should be aligned by specific amount (C++11)
aligned_storage defines the type suitable for use as uninitialized storage for types of given size