Thread: Question about the C99 standard (concerning union)

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    Question about the C99 standard (concerning union)

    Hi,

    I'm writing code that works, but in such a nasty way...

    Code:
    typedef union Pixel
    {
        struct
        {
            unsigned bit0:1;
            unsigned bit1:1;
            unsigned bit2:1;
            unsigned bit3:1;
            unsigned bit4:1;
            unsigned bit5:1;
            unsigned bit6:1;
            unsigned bit7:1;
        } field;
        unsigned ch:8;
    } Pixel;
    Then I initialize Pixels using px.field.bit0 = 0, px.field.bit1 = 1, ....
    And I'd like to know if the standard allows me then, to compare two Pixels using the ch member of the union:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    typedef union Pixel
    {
        struct
        {
            unsigned bit0:1;
            unsigned bit1:1;
            unsigned bit2:1;
            unsigned bit3:1;
            unsigned bit4:1;
            unsigned bit5:1;
            unsigned bit6:1;
            unsigned bit7:1;
        } field;
        unsigned ch:8;
    } Pixel;
    
    int main()
    {
        Pixel p[3];
    
        /* Initializing, using field.bitX */
        p[0].field.bit0 = 1, p[0].field.bit1 = 0, p[0].field.bit2 = 1,  p[0].field.bit3 = 1,  p[0].field.bit4 = 1,  p[0].field.bit5 = 1,  p[0].field.bit6 = 1,  p[0].field.bit7 = 1;
        p[1].field.bit0 = 1, p[1].field.bit1 = 1, p[1].field.bit2 = 1,  p[1].field.bit3 = 1,  p[1].field.bit4 = 1,  p[1].field.bit5 = 1,  p[1].field.bit6 = 1,  p[1].field.bit7 = 1;
        p[2].field.bit0 = 1, p[2].field.bit1 = 0, p[2].field.bit2 = 1,  p[2].field.bit3 = 1,  p[2].field.bit4 = 1,  p[2].field.bit5 = 1,  p[2].field.bit6 = 1,  p[2].field.bit7 = 1;
    
        if (p[0].ch == p[1].ch)
        {
            puts("p[0] == p[1]");
        }
        if (p[0].ch == p[2].ch)
        {
            puts("p[0] == p[2]");
        }
    
        return 0;
    }
    As you see, I only use the "ch" part to compare two Pixels, it is never used to set a Pixel to some value.

    I'm really afraid it may bug some day :s

    Does the C99 standard allow me to do so?

    Cheers.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That should work [I think]. Note however that differnet machines may order the bits in a word differently.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I don't know what the standard says on the subject, but sizeof() your
    structure returns 4, meaning it allocates an int (as expected as
    you simply declare your fields as 'unsigned [int]'), perhaps you
    should precise 'unsigned char' to allocate only what you need (i.e. 1 byte).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > And I'd like to know if the standard allows me then, to compare two Pixels using the ch member of the union:
    In a word - no.

    The standard makes it pretty clear that almost everything about bit-fields is implementation specific. Like for example which end of the word the bits are packed in, where the padding and alignment is, what triggers the next bit-field to be stored in the next word etc etc.

    Whenever you start trying to access a bit-field by some other method, or assuming that you can overlay some external data on top a bit-field, then you're certain to come unstuck at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    7
    ok thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main() return value standard - stupid question
    By shuuhen in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 01:45 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  5. Question about no '.h' standard
    By JLBSchreck in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2003, 02:26 PM