class CHaraICICCC
{
int i;
char c1;
int j;
char c2;
char c3;
char c4;
};
class CHaraIICCCC
{
int i;
int j;
char c1;
char c2;
char c3;
char c4;
};
void fun()
{
CHaraICICCC eici;
CHaraIICCCC eiicc;
int icic = sizeof(eici); // -> output of icic is 16.
int iicc = sizeof(eiicc); // -> output of icic is 12.
}
If any one knows, Please let me know why like this.
Thanks
Hara
Because of alignment. x86 compilers tend to align int types on 4 bytes boundary (for faster memory access) so CHaraICICCC would probably be laid out as this:
for a total of 15 bytes, while CHaraIICCCC would be:
for a total of 12 bytes (with no bytes wasted for padding). Of course, this is much compiler-related and dependant on your compilation options.