I was trying to solve one exam question, where question was asked that what is the sizeof class A and class B?
class A {
char c;
short s;
char c2;
int i;
};
class B {
int i;
short s;
char c, c2;
};
As per me
sizeof class A : 8 (char:1, short:2, char:1, int:4 -- No Padding)
sizeof class B : 8 (int:4, short:2, char:1+1 -- No Padding)
When I tried in my system then I got different output than what I was expecting
sizeof class A : 12
sizeof class B : 8
When I made some changes in the class
class A {
char c,c2;
short s;
//char c2;
int i;
};
class B {
int i;
short s;
char c, c2;
};
Then Output was sizeof(A): 8 and sizeof(B): 8
Can someone please explain me that how output is 12 & 8? Thanks in advance