0

With the below code:

struct S {
unsigned char b1 : 3;
int i;
};

I can deduce that char b1 is of 1 byte and using 3 bits of that and int is 4 bytes but when I try to print out the size of S, I get it 8 bytes instead of 5 bytes(1 for char+4 for int) as I expected and this is something I am not understanding by obvious. Can something explain why size of S is 8 bytes?

user207421
  • 305,947
  • 44
  • 307
  • 483
Vinod
  • 115
  • 11
  • 2
    http://stackoverflow.com/questions/6968468/padding-in-structures-in-c?lq=1 – Mat Apr 19 '16 at 04:36
  • run this code on vc++ and gcc compiler and result is same. – Vinod Apr 19 '16 at 04:37
  • 1
    And the reason is the same too. Have you read the link? Or [this one](http://stackoverflow.com/questions/12761994/size-of-a-structure-containing-bit-fields?rq=1)? Or [this](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member)? – user207421 Apr 19 '16 at 04:38

0 Answers0