What's the difference between sizeof and alignof?

CServer Side ProgrammingProgramming

Here we will see what are the differences of sizeof and the alignof operator in C++. The alognof() operator is introduced in C++11.

The alignof() operator is used to get the alignment in bytes. It requires instances of type. the type is either complete type or a reference type. There is another operator called the sizeof() operator, that returns the size of one type. For normal datatypes the sizeof and the alignof returns the same value. For some user defined datatype, the alignof returns some different value. Let us see the example to get the idea.


 Live Demo

using namespace std;
struct MyStruct{
   int x;
   double y;
   char z;
main() {
   cout << "The sizeof(MyStruct): " << sizeof(MyStruct) << endl;
   cout << "The alignof(MyStruct): " << alignof(MyStruct) << endl;


The sizeof(MyStruct): 24
The alignof(MyStruct): 8
Updated on 30-Jul-2019 22:30:26