Thread: bit fields

  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
    28,413
    There is padding for alignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 08: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, 02: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, 02:01 PM

Tags for this Thread