Bit-field Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fao, Sean

    Bit-field Question

    I was just thumbing through K&R 2nd Edition and thought I'd read up on
    bit-fields, which I [personally] haven't had much use for in my career.
    Anyhow, K&R says that, "Fields may be declared only as ints; for
    portability, specify signed or unsigned explicitly". So it got me
    wondering what would happen if I tried to declare a field as a short
    rather than an int.

    I compiled my code with GCC 3.3.6 with all warning turned on and I got
    no warnings returned. Have things changed since K&R or is there
    something bad about my code?

    On my implementation, the sizeof() each bit-field is identical to the
    type I used to declare the fields.

    <code>
    #include <stdio.h>

    struct {
    unsigned short admin : 1;
    unsigned short manager : 1;
    unsigned short supervisor : 1;
    unsigned short user : 1;
    unsigned short guest : 1;
    } roles;

    struct {
    unsigned int admin : 1;
    unsigned int manager : 1;
    unsigned int supervisor : 1;
    unsigned int user : 1;
    unsigned int guest : 1;
    } roles2;

    int main(void)
    {
    printf("sizeof( short): %d sizeof(roles): %d\n",
    sizeof(short), sizeof(roles));
    printf("sizeof( int): %d sizeof(roles2): %d\n",
    sizeof(int), sizeof(int));


    return 0;
    }
    </code>

    Just curious...

    Thank you in advance,

    --
    Sean
  • Keith Thompson

    #2
    Re: Bit-field Question

    "Fao, Sean" <enceladus311@y ahoo.comI-WANT-NO-SPAM> writes:[color=blue]
    > I was just thumbing through K&R 2nd Edition and thought I'd read up on
    > bit-fields, which I [personally] haven't had much use for in my
    > career. Anyhow, K&R says that, "Fields may be declared only as ints;
    > for portability, specify signed or unsigned explicitly". So it got me
    > wondering what would happen if I tried to declare a field as a short
    > rather than an int.[/color]

    What will happen depends on the implementation. The standard says
    (C99 6.7.2.1p4):

    A bit-field shall have a type that is a qualified or unqualified
    version of _Bool, signed int, unsigned int, or some other
    implementation-defined type.

    An implementation may support other types for bit fields, but it's not
    required to; any program that uses such an extension is non-portable.
    [color=blue]
    > I compiled my code with GCC 3.3.6 with all warning turned on and I got
    > no warnings returned. Have things changed since K&R or is there
    > something bad about my code?
    >
    > On my implementation, the sizeof() each bit-field is identical to the
    > type I used to declare the fields.[/color]

    C99 6.5.3.4p1:

    The sizeof operator shall not be applied to an expression that has
    function type or an incomplete type, to the parenthesized name of
    such a type, or to an expression that designates a bit-field
    member.

    This is a constraint, so if gcc doesn't give you a diagnostic, it's
    non-conforming. (gcc can generally be persuaded to be conforming, or
    nearly so, with command-line options.)

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Jack Klein

      #3
      Re: Bit-field Question

      On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-u@mib.org> wrote
      in comp.lang.c:

      [snip]
      [color=blue][color=green]
      > > On my implementation, the sizeof() each bit-field is identical to the
      > > type I used to declare the fields.[/color]
      >
      > C99 6.5.3.4p1:
      >
      > The sizeof operator shall not be applied to an expression that has
      > function type or an incomplete type, to the parenthesized name of
      > such a type, or to an expression that designates a bit-field
      > member.
      >
      > This is a constraint, so if gcc doesn't give you a diagnostic, it's
      > non-conforming. (gcc can generally be persuaded to be conforming, or
      > nearly so, with command-line options.)[/color]

      You missed on this last one, Keith. His remark, which you are
      commenting on here, is inaccurate and does not reflect what his code
      actually does. I have replaced his code below, which you snipped:
      [color=blue]
      > #include <stdio.h>
      >
      > struct {
      > unsigned short admin : 1;
      > unsigned short manager : 1;
      > unsigned short supervisor : 1;
      > unsigned short user : 1;
      > unsigned short guest : 1;
      > } roles;
      >
      > struct {
      > unsigned int admin : 1;
      > unsigned int manager : 1;
      > unsigned int supervisor : 1;
      > unsigned int user : 1;
      > unsigned int guest : 1;
      > } roles2;
      >
      > int main(void)
      > {
      > printf("sizeof( short): %d sizeof(roles): %d\n",
      > sizeof(short), sizeof(roles));
      > printf("sizeof( int): %d sizeof(roles2): %d\n",
      > sizeof(int), sizeof(int));
      >
      >
      > return 0;
      > }[/color]

      Clearly he is not applying the sizeof operator to the bit-field
      members, but to the entire struct type containing the bit-field
      members, and this of course is perfectly valid.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Keith Thompson

        #4
        Re: Bit-field Question

        Jack Klein <jackklein@spam cop.net> writes:[color=blue]
        > On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-u@mib.org> wrote
        > in comp.lang.c:
        >
        > [snip]
        >[color=green][color=darkred]
        >> > On my implementation, the sizeof() each bit-field is identical to the
        >> > type I used to declare the fields.[/color]
        >>
        >> C99 6.5.3.4p1:
        >>
        >> The sizeof operator shall not be applied to an expression that has
        >> function type or an incomplete type, to the parenthesized name of
        >> such a type, or to an expression that designates a bit-field
        >> member.
        >>
        >> This is a constraint, so if gcc doesn't give you a diagnostic, it's
        >> non-conforming. (gcc can generally be persuaded to be conforming, or
        >> nearly so, with command-line options.)[/color]
        >
        > You missed on this last one, Keith. His remark, which you are
        > commenting on here, is inaccurate and does not reflect what his code
        > actually does. I have replaced his code below, which you snipped:[/color]

        [snip again]
        [color=blue]
        > Clearly he is not applying the sizeof operator to the bit-field
        > members, but to the entire struct type containing the bit-field
        > members, and this of course is perfectly valid.[/color]

        Whoops.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Roberto Waltman

          #5
          Re: Bit-field Question

          <enceladus311@y ahoo.comI-WANT-NO-SPAM> wrote:[color=blue]
          >...
          >...
          >int main(void)
          >{
          > printf("sizeof( short): %d sizeof(roles): %d\n",
          > sizeof(short), sizeof(roles));
          > printf("sizeof( int): %d sizeof(roles2): %d\n",
          > sizeof(int), sizeof(int));[/color]
          -------------------------------^^^

          You probably meant "roles2" here.


          Roberto Waltman

          [ Please reply to the group,
          return address is invalid ]

          Comment

          • Keith Thompson

            #6
            Re: Bit-field Question

            Roberto Waltman <usenet@rwaltma n.net> writes:[color=blue]
            > <enceladus311@y ahoo.comI-WANT-NO-SPAM> wrote:[color=green]
            >>...
            >>...
            >>int main(void)
            >>{
            >> printf("sizeof( short): %d sizeof(roles): %d\n",
            >> sizeof(short), sizeof(roles));
            >> printf("sizeof( int): %d sizeof(roles2): %d\n",
            >> sizeof(int), sizeof(int));[/color]
            > -------------------------------^^^
            >
            > You probably meant "roles2" here.[/color]

            Apart from that, "%d" isn't a correct format for size_t, which is the
            type that the sizeof operator yields. Either use "%ld" and cast the
            value to unsigned long, or use the C99-specific "%zd" format.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            Working...