Learn++ Lesson 48: The Alignas Keyword in C++ Programming
In C++, the `alignas` keyword specifies the memory alignment of a variable or structure. This lesson combines `alignas(16)` with `alignof` so learners can see the requested alignment in a small, compilable example.
Using alignas for structure alignment
#include <iostream>
struct alignas(16) AlignedStruct {
int x;
};
int main() {
std::cout << alignof(AlignedStruct);
return 0;
}
Output:
16
The `AlignedStruct` type is requested to begin on a 16-byte boundary. The `alignof` operator reports the alignment requirement, so this program prints `16`.
Why alignas matters
- It requests a specific alignment for an object or type.
- It is useful in systems programming and performance-sensitive code.
- It works with `alignof` to inspect alignment requirements.
What you practice in the app
- Applying `alignas(16)` to a structure.
- Reading the `alignof` operator.
- Connecting a type definition to its alignment output.
Today's challenge
Fill in the missing type name so the program calls `alignof` for the aligned structure and prints `16`. The unique intended answer is `AlignedStruct`.