alignof operator in C++ (original) (raw)
Last Updated : 9 Jun, 2026
The alignof operator was introduced in C++11 to determine the alignment requirement of a type in memory. It returns the number of bytes by which objects of a particular type must be aligned for efficient memory access.
- Returns the alignment requirement of a type in bytes
- The returned value is typically of type std::size_t
- Works with built-in types, arrays, references, classes, and structures CPP `
#include using namespace std;
struct Geeks { int i; float f; char s; };
struct Empty { };
// driver code int main() { cout << "Alignment of char: " << alignof(char) << endl; cout << "Alignment of pointer: " << alignof(int*) << endl; cout << "Alignment of float: " << alignof(float) << endl; cout << "Alignment of class Geeks: " << alignof(Geeks) << endl; cout << "Alignment of Empty class: " << alignof(Empty) << endl;
return 0;}
`
Output
Alignment of char: 1 Alignment of pointer: 8 Alignment of float: 4 Alignment of class Geeks: 4 Alignment of Empty class: 1
**Explanation
- The alignof operator returns the alignment requirement of a type in bytes.
- char, float, and int* have alignment requirements of 1, 4, and 8 bytes respectively (on most 64-bit systems).
- For the Geeks structure, the compiler uses the largest alignment requirement among its members, which is 4 bytes.
- Therefore, alignof(Geeks) returns 4.
Syntax
alignof(type)
**Parameters: The type can be:
- A complete type (such as int, float, or a user-defined type)
- An array type, in which case the alignment requirement of the element type is returned
- A reference type, in which case the alignment requirement of the referenced type is returned
**Return Value: The alignof operator returns the alignment requirement of the specified type in bytes. The returned value is of type std::size_t.
Difference Between alignof and sizeof
The alignof operator returns the alignment requirement of a type, while the sizeof operator returns the amount of memory occupied by a type or object.
- alignof determines how data should be aligned in memory.
- sizeof determines how many bytes a type occupies.
- For fundamental types, both values are often the same.
- For structures and classes, the values can differ due to padding and alignment requirements.
**Example:
C++ `
struct S { int a; double b; };
// alignof(S) == 8
`
- The alignment requirement of S is 8 bytes.
- This is because double has the strictest alignment requirement among the structure members.
**Example: alignof vs sizeof
CPP `
#include using namespace std;
struct Geeks { int i; float f; char s; };
int main() {
cout << "alignment of Geeks : " << alignof(Geeks) << '\n';
cout << "sizeof of Geeks : " << sizeof(Geeks) << '\n';
cout << "alignment of int : " << alignof(int) << '\n';
cout << "sizeof of int : " << sizeof(int) << '\n';}
`
Output
alignment of Geeks : 4 sizeof of Geeks : 12 alignment of int : 4 sizeof of int : 4
**Explanation
- alignof(Geeks) returns 4, which is the alignment requirement of the structure.
- sizeof(Geeks) returns 12, which is the total memory occupied by the structure.
- alignof(int) and sizeof(int) both return 4.
- This demonstrates that alignment requirements and memory size are not always the same.