C _Alignas Keyword
Example
Give a structure member an alignment of 16 bytes:
struct myStructure {
_Alignas(16) int myNum;
};
int main() {
printf("%zu", _Alignof(struct myStructure));
return 0;
}
Definition and Usage
The _Alignas keyword specifies the alignment of a variable or a structure member. The alignment is a number of bytes. A value with alignment 16 always starts at a memory address that is a multiple of 16.
Some hardware and some fast CPU instructions need data with a special alignment. Most programs do not need _Alignas, because the compiler chooses a good alignment for every type.
In the example, myNum has alignment 16, so the whole structure also gets alignment 16, and _Alignof returns it. If you include <stdalign.h>, you can write alignas instead of _Alignas.
Related Pages
The _Alignof keyword returns the alignment of a type.
Read more about alignment in our C Struct Alignment and Padding Tutorial.