Volatile bitfields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexis4
    New Member
    • Dec 2009
    • 113

    #1

    Volatile bitfields

    Hi!

    What is the difference between the below 3 cases in ANSI C?

    Code:
    typedef struct
    {
    [B]volatile [/B]unsigned char a : 1,
                           b : 1,
                           c : 1,
                           d : 1,
                           e : 1,
                           f : 1,
                           g : 1,
                           h : 1;
    } MyStruct1;
    MyStruct1    struct1;
    
    
    typedef struct
    {
    unsigned char a : 1,
                  b : 1,
                  c : 1,
                  d : 1,
                  e : 1,
                  f : 1,
                  g : 1,
                  h : 1;
    } MyStruct2;
    [B]volatile [/B]MyStruct2    struct2;
    
    
    typedef struct
    {
    [B]volatile [/B]unsigned char a : 1,
                           b : 1,
                           c : 1,
                           d : 1,
                           e : 1,
                           f : 1,
                           g : 1,
                           h : 1;
    } MyStruct3;
    [B]volatile [/B]MyStruct3    struct3;

    I know what a volatile and what a bitfield is, so it is not necessary to explain those.

    Furthermore, I think that struct1, struct2 and struct3 are exactly the same, but I'm not sure and that's why I'm asking!


    Thanks in advance!
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    I'm not an expert on bit fields, but I thought C89 only allows bit fields of types unsigned int, signed int, or int; and that C99 added type _Bool. I wouldn't have thought type unsigned char was allowed.

    As to your specific question, I agree that the three declarations are effectively equivalent because there is always at least one volatile in scope for every bit field for every case.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Many many compilers allow other types for bitfields as extensions so it is common to be able to use char, short or long based bitfields. That said bit fields are so platform dependent in implementation, order bits from the type are used in position and number of padding bits that I tend to stick clear of bitfield all together, especially as they are just syntactic sugar for the bitwise operators which you can use an write fully portable code.

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        Thank you both for your answers!

        Regards,
        Alexis

        Comment

        Working...