C++ alignof Keyword
The alignof keyword in C++ is used to determine the alignment requirement of a type or object. It returns the alignment value in bytes, which indicates how the type is aligned in memory.
The alignof keyword was introduced in C++11. It helps in understanding and managing memory layouts, especially in performance-critical applications.
Alignment refers to the memory boundaries on which a type or object is placed. For instance, an int may have an alignment requirement of 4 bytes, meaning it should start at a memory address divisible by 4.
Syntax
alignof(type);
- type
- The data type or object for which the alignment is queried.
Examples
Example 1: Checking Alignment of Basic Types
This example demonstrates how to use alignof to check the alignment of fundamental data types.
#include <iostream>
using namespace std;
int main() {
cout << "Alignment of int: " << alignof(int) << " bytes" << endl;
cout << "Alignment of double: " << alignof(double) << " bytes" << endl;
cout << "Alignment of char: " << alignof(char) << " bytes" << endl;
return 0;
}
Output:
Alignment of int: 4 bytes
Alignment of double: 8 bytes
Alignment of char: 1 bytes
Explanation:
- The
alignofkeyword queries the alignment requirements ofint,double, andchar. - The output shows the alignment values based on the architecture and compiler being used.
- For instance, an
intis aligned to a 4-byte boundary, and acharis aligned to a 1-byte boundary.
Example 2: Alignment of User-Defined Structures
This example demonstrates how alignof works with structures containing multiple members.
#include <iostream>
using namespace std;
struct MyStruct {
char c;
int i;
double d;
};
int main() {
cout << "Alignment of MyStruct: " << alignof(MyStruct) << " bytes" << endl;
return 0;
}
Output:
Alignment of MyStruct: 8 bytes
Explanation:
- The structure
MyStructcontains achar,int, anddouble. - The alignment of the structure is determined by its largest member, which is
doublewith an alignment of 8 bytes. - Padding may be added to meet the alignment requirements.
Example 3: Using alignof with Aligned Types
This example demonstrates how alignof works with types that have custom alignment using alignas.
#include <iostream>
using namespace std;
struct alignas(16) AlignedStruct {
int data;
};
int main() {
cout << "Alignment of AlignedStruct: " << alignof(AlignedStruct) << " bytes" << endl;
return 0;
}
Output:
Alignment of AlignedStruct: 16 bytes
Explanation:
- The structure
AlignedStructis declared with an alignment of 16 bytes usingalignas(16). - The
alignofkeyword verifies that the alignment of the structure is 16 bytes as specified. - Custom alignment is particularly useful for optimizing performance in specific applications like SIMD.
Key Points about alignof Keyword
- The
alignofkeyword determines the alignment requirements of a type or object. - It returns the alignment in bytes, which is typically a power of 2.
- It is useful for understanding memory layouts and ensuring proper alignment in performance-critical applications.
- The alignment of user-defined types is determined by the largest alignment requirement of their members.
alignofcan be combined withalignasto create custom-aligned types.
