Object alignment requirement
cppreference Object # alignment requirement
Every object type has the property called alignment requirement, which is an integer value (of type std::size_t, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated.
since C++11
The alignment requirement of a type can be queried with alignof or std::alignment_of. The pointer alignment function std::align can be used to obtain a suitably-aligned pointer within some buffer, and std::aligned_storage can be used to obtain suitably-aligned storage.
Each object type imposes its alignment requirement on every object of that type; stricter alignment (with larger alignment requirement) can be requested using alignas
(since C++11).
In order to satisfy alignment requirements of all non-static members of a class, padding may be inserted after some of its members.
stackoverflow Order between STDCPP_DEFAULT_NEW_ALIGNMENT and alignof(std::max_align_t)
With GCC and Clang on x86-64/Linux alignof(std::max_align_t)
and __STDCPP_DEFAULT_NEW_ALIGNMENT__
are both equal to 16
.
With MSVC on x86-64/Windows alignof(std::max_align_t)
is 8
and __STDCPP_DEFAULT_NEW_ALIGNMENT__
is 16
.
The standard defines the two terms corresponding to these quantities in [basic.align]/3:
An extended alignment is represented by an alignment greater than
alignof(std::max_align_t)
. [...] A type having an extended alignment requirement is an over-aligned type. [...] A new-extended alignment is represented by an alignment greater than__STDCPP_DEFAULT_NEW_ALIGNMENT__
.
A
1、std::max_align_t
: The alignment of the biggest scalar type
2、__STDCPP_DEFAULT_NEW_ALIGNMENT__
: The alignment of allocated memory
NOTE:
C++17引入