-2

I'm facing a strange result sizeof() during testing. How T1 and T2 are the same size as the types used in T2 is smaller than T1?

#include <iostream>

using namespace std;

struct T1 {
    int     id;
    int     enable;
};

struct T2 {
    int     id;
    char    enable;
};


int main() {
    cout << sizeof(T1) << endl; // Print 8
    cout << sizeof(T2) << endl; // Print 8
    return 0;
}
erfani
  • 9
  • 1

1 Answers1

1

T2 is padded for alignment.

That is, it contains unused bytes such that an array of T2 will have all the T2.id dword aligned.

Jasen
  • 11,837
  • 2
  • 30
  • 48