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.

#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

Syntax

alignof(type)

**Parameters: The type can be:

**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.

**Example:

C++ `

struct S { int a; double b; };

// alignof(S) == 8

`

**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