C++ alignof Keyword
Example
Print the alignment of two types:
struct alignas(16) MyStruct {
int myNum;
char myLetter;
};
int main() {
cout << alignof(char) << "\n";
cout << alignof(MyStruct);
return 0;
}
Definition and Usage
The alignof keyword returns the alignment of a type, in bytes. The alignment means that a value of this type must start at a memory address that is a multiple of this number.
Most results depend on the computer. For example, alignof(int) is often 4, but not on every computer. alignof(char) is always 1.
Use the alignas keyword to set the alignment of a type yourself, like for MyStruct in the example above.
Related Pages
Read more about data types in our C++ Data Types Tutorial.