To understand alignas
and alignof
you must know What data alignment mean
Good guide for it https://developer.ibm.com/articles/pa-dalign//
Alignment (nutshell)
Explanation 1
Data alignment means putting the data in memory at address equal to some multiple of the word size.
Explanation 2
Alignment is a property of a memory address, expressed as the numeric address modulo a power of 2. For example, the address 0x0001103F modulo 4 is 3. That address is said to be aligned to 4n+3, where 4 indicates the chosen power of 2. The alignment of an address depends on the chosen power of 2. The same address modulo 8 is 7. An address is said to be aligned to X if its alignment is Xn+0.
CPUs execute instructions that operate on data stored in memory. The data are identified by their addresses in memory. A single datum also has a size. We call a datum naturally aligned if its address is aligned to its size. It's called misaligned otherwise. For example, an 8-byte floating-point datum is naturally aligned if the address used to identify it has an 8-byte alignment.
Okay. You understood "data alignment"
Congratulations!
What does mean alignas
Explanation
alignas (N)
specifies will place data only in address of a multiple of N
N
- the number modulo a power of 2
Syntax:
alignas( the numeric address modulo a power of 2 )
alignas( alignof(type-id) )
alignas( type-id )
The alignas specifier may be applied to:
the declaration or definition of a class
/ struct
/ union
or enumeration
;
the declaration of a non-bitfield class data member;
the declaration of a variable, except that it cannot be applied to the following:
- a function parameter;
- the exception parameter of a catch clause.
Example:
struct alignas(256) name1 // every object of type name1 will be aligned to 256-byte boundary
{
float test[4];
};
alignas(128) char name2[128]; // the array "name2" will be aligned to 128-byte boundary
Addition 1
The alignas
type specifier is a portable, C++ standard way to
specify custom alignment of variables and user defined types.
Addition 2
#include <iostream>
struct alignas(16) Bar
{
int i; // 4 bytes
int n; // 4 bytes
alignas(4) char arr[3];
short s; // 2 bytes
};
int main()
{
std::cout << alignof(Bar) << std::endl;
}
When multiple alignas specifiers are encountered, the compiler will choose the strictest one, (the one with the largest value).
output: 16
Addition 3
alignas
cannot be used to give a type a smaller alignment than the
type would have without this declaration
What does mean alignof
Syntax:
alignof( type-id )
Returns a value of type std::size_t
Same definition have sizeof( type-id )
What's the difference between sizeof
and alignof
?
struct MyStruct
{
int x;
double y;
char z;
};
main()
{
std::cout << "The sizeof(MyStruct): " << sizeof(MyStruct) << std::endl;
std::cout << "The alignof(MyStruct): " << alignof(MyStruct) << std::endl;
}
output:
The sizeof(MyStruct): 24
The alignof(MyStruct): 8
Problem in structure padding
Structure padding is a concept in C that adds the one or more empty
bytes between the memory addresses to align the data in memory
More information: Struct padding in C++
Addition
The result is a constant expression of type std::size_t
, i.e., it can
be evaluated at compile time.
More information here:
Source 1,
Source 2,
alignof
page (it does now, on the work-in-progress object page). I don't see how cplusplus.com is relevant. – Sommervilleint
– Elemental