Thread: Bit Fields example.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Bit Fields example.

    Hello to all. There is a point that I can't understand.

    Code:
    struct s {
    unsigned int a: 4;
    unsigned int: 0;
    unsigned int b: 8;
    }
    a 0-BIT FIELD is a signal to the compiler to align the following bit field at the beginning of a storage unit if storage units are 8 bits long the compiler will allocate 4 bits for the a member , skip 4 bits to the next storage unit and then allocate 8 bits for b.If storage units are 16bit long the compiler will allocate 4 bits for a skip 12 bits and then allocate 8 bits for b.

    I can't understand the layout. Compiler will allocate 4 bits for the a member in the first case and skip the 4 bits to the next storage but we have 8bits long storage unit the remaining is padding? "skip 4 bits to the next storage unit" from where is these 4 bits? where come from?

    In the second case from where we have 12 bits?
    I am little confusing about how memory layout is organized when we are dealing with bit fields

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Compiler will allocate 4 bits for the a member in the first case and skip the 4 bits to the next storage but we have 8bits long storage unit the remaining is padding? "skip 4 bits to the next storage unit" from where is these 4 bits? where come from?
    Yes, the gap for alignment is padding.

    Quote Originally Posted by Mr.Lnx
    In the second case from where we have 12 bits?
    16-4=12, where 16 is the number of bits of a "storage unit" and 4 is the number of bits for the member named a.
    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
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Code:
    #include <stdio.h>
    typedef struct s {
    unsigned int a: 4;
    unsigned int: 0;
    unsigned int b: 8;
    } my_s;
    
    int main(void) {
    	
    	printf("%d , %d" , sizeof(unsigned int) , sizeof(my_s));
    	
    	return 0;
    }
    I take 4 and 8 size of unsigned int and my_s structure respectively the unnamed field is off? I don't understand how the compiler reduce the size. We have 3 members here 4 + 4 + 4 = 12 bytes I think.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    I take 4 and 8 size of unsigned int and my_s structure respectively the unnamed field is off? I don't understand how the compiler reduce the size. We have 3 members here 4 + 4 + 4 = 12 bytes I think.
    sizeof gives the size in bytes, not bits. Assuming that CHAR_BIT == 8, which is almost certainly true, and assuming that an int is the size of a "storage unit", then you have 32-bit storage units, so the compiler will allocate 4 bits for the member named a, skip 32-4=28 bits, then allocate 8 bits for the member named b, hence the 8 bytes in total after padding.
    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

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Yes I meant that each unsigned int occupies 4 bytes in the memory so the result of sizeof(my_s) I expected instead of 12 bytes not 8 that is why I think I have no understand the concept of bit fields. How many unit storage's would compiler need? 2? It is difficult to understand the concept I really need a schema/picture or an output of assembly code although I don't know assembly :/

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Yes I meant that each unsigned int occupies 4 bytes in the memory so the result of sizeof(my_s) I expected instead of 12 bytes not 8
    The member named a is in the first 4 bytes; the member named b is in another 4 bytes. Why would there be yet another 4 bytes? It cannot be for the unnamed 0-bit member that is used merely to "signal to the compiler to align the following bit field at the beginning of a storage unit".
    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

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    So the unnamed 0-bit member does not occupy any memory ok.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    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

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok.So we have 8 * 8 = 64bits hence 2 storage units of 32. The layout would be :

    Code:
           
     
        |__4 bits for a member___|___28bits padding___|           |___8bits for member b____|__24bits padding__|
     
                    1st 32 bit storage unit                                                                    2nd 32bit storage unit 
    Is that true?
    Last edited by Mr.Lnx; 06-11-2015 at 05:23 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    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

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by laserlight View Post
    Yes.
    Thank you very much for your time laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit-Fields in C.
    By Mr.Lnx in forum C Programming
    Replies: 5
    Last Post: 06-29-2014, 07:22 AM
  2. bit fields
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 03-21-2013, 01:28 AM
  3. Bit fields
    By Edelweiss in forum C Programming
    Replies: 5
    Last Post: 08-23-2011, 10:01 AM
  4. bit fields before non-bit fields . . .
    By dwks in forum C Programming
    Replies: 10
    Last Post: 10-13-2005, 02:36 AM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 02:01 PM