Like Tree1Likes
  • 1 Post By rags_to_riches

bit fields

This is a discussion on bit fields within the C Programming forums, part of the General Programming Boards category; the foll code when run on gcc compiler gave 12 as output Code: struct aa{char a:3;int b:30;char c:3;}; printf("%d",sizeof(struct aa)); ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    bit fields

    the foll code when run on gcc compiler gave 12 as output
    Code:
     struct aa{char a:3;int b:30;char c:3;};
        printf("%d",sizeof(struct aa));
    but since sizeof int is 4 and char is 1 then why does it give 12 instead of 8 as output????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    There is padding for alignment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Member rearrangement may coax it to use a byte alignment instead of a word alignment.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    Quote Originally Posted by whiteflags View Post
    Member rearrangement may coax it to use a byte alignment instead of a word alignment.
    Member rearrangement is rarely sufficient to coax a compiler to use byte alignment - some other settings (compilation options) are also needed. And that requires all functions in your program to be compiled in the same manner - mixing and matching functions compiled with different alignment settings rarely works.

    On a (fairly) typical 32 bit system that aligns on 32 bit boundaries, it wouldn't make much difference in this case.

    But, yeah, in general, arranging members in order of decreasing size is likely to result in smaller overall struct than other orderings.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    But when I replace sizeof c with 2 bits then it gives 8 as output ..Can someone please explain how is this possible???

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Salem likes this.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    Quote Originally Posted by Saurabh Mehta View Post
    But when I replace sizeof c with 2 bits then it gives 8 as output ..Can someone please explain how is this possible???
    It probably means the compiler is smart enough to pack b and c into one 32-bit word (which isn't possible if c is 3 bits, as 33 bits won't fit in a 32 bit word). a is in another 32 bit word. With 8-bit bytes, that means sizeof your struct yields 8.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by grumpy View Post
    It probably means the compiler is smart enough to pack b and c into one 32-bit word (which isn't possible if c is 3 bits, as 33 bits won't fit in a 32 bit word). a is in another 32 bit word. With 8-bit bytes, that means sizeof your struct yields 8.
    Then why doesnt it do the same thing when only 1 bit is increased I mean why on increasing size with just one bit it straight away increases the total size by 4 bytes??

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    Quote Originally Posted by Saurabh Mehta View Post
    Then why doesnt it do the same thing when only 1 bit is increased I mean why on increasing size with just one bit it straight away increases the total size by 4 bytes??
    Refer post 2.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By juice in forum C Programming
    Replies: 7
    Last Post: 12-13-2011, 07:38 AM
  2. Bit fields
    By Edelweiss in forum C Programming
    Replies: 5
    Last Post: 08-23-2011, 10:01 AM
  3. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 01:38 PM
  4. bit-fields
    By kps in forum C Programming
    Replies: 9
    Last Post: 09-25-2002, 08:25 PM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 01:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21