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

</>
Copy
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.

</>
Copy
#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:

  1. The alignof keyword queries the alignment requirements of int, double, and char.
  2. The output shows the alignment values based on the architecture and compiler being used.
  3. For instance, an int is aligned to a 4-byte boundary, and a char is aligned to a 1-byte boundary.

Example 2: Alignment of User-Defined Structures

This example demonstrates how alignof works with structures containing multiple members.

</>
Copy
#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:

  1. The structure MyStruct contains a char, int, and double.
  2. The alignment of the structure is determined by its largest member, which is double with an alignment of 8 bytes.
  3. 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.

</>
Copy
#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:

  1. The structure AlignedStruct is declared with an alignment of 16 bytes using alignas(16).
  2. The alignof keyword verifies that the alignment of the structure is 16 bytes as specified.
  3. Custom alignment is particularly useful for optimizing performance in specific applications like SIMD.

Key Points about alignof Keyword

  1. The alignof keyword determines the alignment requirements of a type or object.
  2. It returns the alignment in bytes, which is typically a power of 2.
  3. It is useful for understanding memory layouts and ensuring proper alignment in performance-critical applications.
  4. The alignment of user-defined types is determined by the largest alignment requirement of their members.
  5. alignof can be combined with alignas to create custom-aligned types.