Alignas in C++ 11 (original) (raw)

Last Updated : 9 Jun, 2026

The alignas specifier was introduced in C++11 to provide custom memory alignment for variables and user-defined types such as classes and structures. Proper alignment can improve memory access efficiency and help optimize performance on modern hardware architectures.

Syntax

alignas( alignment_value )

where, alignment_value is an integral constant expression that specifies the desired alignment in bytes.

Applicability of alignas

The alignas specifier can be applied to:

**Note: The examples below use the alignof operator to determine the alignment requirement of a type. Familiarity with alignof will help in understanding the output.

C++ `

#include using namespace std;

// struct is aligned to 16 bytes in memory. struct alignas(16) Demo {

int var1l; // 4 bytes
int var2; // 4 bytes
short s; // 2 bytes
// char aligned to 4 bytes in memory.
alignas(4) char arr[5];

};

// driver code int main() { cout << alignof(Demo) << endl; // output: 16 return 0; }

`

**Explanation

**Practice: Try to use sizeof(Demo) and compare it with alignof(Demo) and check the difference if any.

Real-Time Use Cases of alignas

The alignas specifier is commonly used in performance-critical applications where memory alignment affects efficiency.