sizes of bit fields

Discussion in 'C Programming' started by Abhi, Jul 5, 2005.

  1. Abhi

    Abhi Guest

    In ANSI C, we can have a bit fields inside structure like

    struct _A {
    int i:2;
    };

    would define a variable of 2-bit length. My doubt is that what would
    the size of this structure then? Would it be sizeof(int) or just 1
    byte? I know that it is quite compiler specific, but what it should be
    according to the ANSI standard? I couldn't find exact documentation
    explaining this.

    Microsoft compiler does give sizeof(int) for such structure.

    Regards,
    -Abhishikt
    Abhi, Jul 5, 2005
    #1
    1. Advertising

  2. Abhi

    David Lago Guest

    Abhi wrote:
    > would define a variable of 2-bit length. My doubt is that what would
    > the size of this structure then? Would it be sizeof(int) or just 1
    > byte?


    The actual size of the struct depends on your compiler, and which amount
    of space it allocates for each word of data. In gcc, it allocates space
    in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So

    actual_size = size + word_size - size % word_size

    This equation does not take in account the padding fields (fields which
    bit size is 0, used so as to move next field to the next word boundary).

    Regards,
    David.
    --
    David Lago <>

    PGP key available at: http://pgp.mit.edu
    Personal Blizog: http://arcanelinux.org/blog/dlworld
    The Enflame Project: http://enflame.org
    David Lago, Jul 5, 2005
    #2
    1. Advertising

  3. Abhi

    Abhi Guest

    The equation you have provided assists my understanding. Just a note
    that, the word_size is not always in block of 4. It depends on what
    data type we use (either int or char).

    typedef struct {
    int a:7;
    int a1:1;
    } AA;

    typedef struct {
    char b:7;
    char b1:1;
    } BB;

    in above two structures AA has sizeof 4 bytes, while BB has 1 byte.

    and another point is that we CANNOT have a variable like "char c:9" it
    generates a compiler error. (at least on Microsoft compiler).

    Regards,
    -Abhishikt
    Abhi, Jul 5, 2005
    #3
  4. Abhi

    David Lago Guest

    Abhi wrote:
    > The equation you have provided assists my understanding. Just a note
    > that, the word_size is not always in block of 4. It depends on what
    > data type we use (either int or char).


    Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
    Ritchie:

    [...] Fields may be declared only as ints; for portability, specify
    signed or unsigned explicitly.

    David.
    --
    David Lago <>

    PGP key available at: http://pgp.mit.edu
    Personal Blizog: http://arcanelinux.org/blog/dlworld
    The Enflame Project: http://enflame.org
    David Lago, Jul 5, 2005
    #4
  5. On Tue, 05 Jul 2005 03:03:52 -0700, Abhi wrote:

    > In ANSI C, we can have a bit fields inside structure like
    >
    > struct _A {
    > int i:2;
    > };
    >
    > would define a variable of 2-bit length. My doubt is that what would
    > the size of this structure then? Would it be sizeof(int) or just 1
    > byte? I know that it is quite compiler specific, but what it should be
    > according to the ANSI standard? I couldn't find exact documentation
    > explaining this.


    The standard leaves the allocation unit for bit-fields up to the
    implementation, which is why it is compiler specific.

    There is little value in putting a single bit-field in a structure (except
    possibly for specific range issues), the value comes when multiple
    bit-fields are packed together.

    > Microsoft compiler does give sizeof(int) for such structure.


    Which is one reasonable choice.

    Lawrence
    Lawrence Kirby, Jul 5, 2005
    #5
  6. On Tue, 05 Jul 2005 04:46:29 -0700, Abhi wrote:

    > The equation you have provided assists my understanding. Just a note
    > that, the word_size is not always in block of 4. It depends on what
    > data type we use (either int or char).
    >
    > typedef struct {
    > int a:7;
    > int a1:1;
    > } AA;
    >
    > typedef struct {
    > char b:7;
    > char b1:1;
    > } BB;


    However this second version is not standard and therefore not portable. C
    only specifies the types int, signed int and unsigned int for bit-fields.
    There is an oddity for plain int for bit fields where the implementation
    can make it signed or unsigned, whereas it is signed in all other contexts.

    > in above two structures AA has sizeof 4 bytes, while BB has 1 byte.


    Your implementation is at liberty to do that but this isn't guaranteed in
    general.

    Lawrence
    Lawrence Kirby, Jul 5, 2005
    #6
  7. Abhi

    Joe Wright Guest

    David Lago wrote:
    > Abhi wrote:
    >
    >>would define a variable of 2-bit length. My doubt is that what would
    >>the size of this structure then? Would it be sizeof(int) or just 1
    >>byte?

    >
    >
    > The actual size of the struct depends on your compiler, and which amount
    > of space it allocates for each word of data. In gcc, it allocates space
    > in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So
    >

    No David, gcc doesn't do that. Try this and tell me what you get..

    #include <stdio.h>

    struct c {
    char c;
    } c;

    struct s {
    short s;
    } s;

    int main(void) {
    printf("Struct c is %d byte, s is %d bytes.\n",
    (int)sizeof (c), (int)sizeof (s));
    return 0;
    }

    I get.. Struct c is 1 byte, s is 2 bytes.

    > actual_size = size + word_size - size % word_size
    >
    > This equation does not take in account the padding fields (fields which
    > bit size is 0, used so as to move next field to the next word boundary).
    >
    > Regards,
    > David.



    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 5, 2005
    #7
  8. Abhi

    David Lago Guest

    I quote myself on a previous post:

    Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
    Ritchie:

    [...] Fields may be declared only as ints; for portability, specify
    signed or unsigned explicitly.

    David.

    Joe Wright wrote:
    > No David, gcc doesn't do that. Try this and tell me what you get..
    >
    > #include <stdio.h>
    >
    > struct c {
    > char c;
    > } c;
    >
    > struct s {
    > short s;
    > } s;
    >
    > int main(void) {
    > printf("Struct c is %d byte, s is %d bytes.\n",
    > (int)sizeof (c), (int)sizeof (s));
    > return 0;
    > }
    >
    > I get.. Struct c is 1 byte, s is 2 bytes.



    --
    David Lago <>

    PGP key available at: http://pgp.mit.edu
    Personal Blizog: http://arcanelinux.org/blog/dlworld
    The Enflame Project: http://enflame.org
    David Lago, Jul 6, 2005
    #8
  9. On Wed, 06 Jul 2005 10:10:23 +0200, David Lago
    <> wrote:

    > I quote myself on a previous post:
    >
    > Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
    > Ritchie:
    >
    > [...] Fields may be declared only as ints; for portability, specify
    > signed or unsigned explicitly.


    Correct but irrelevant. The fields must be declared as int (in C90; C99
    also allows _Bool), so:

    struct fred
    {
    int x : 2; /* OK */
    char y : 1; /* error */
    float z : 12; /* error */
    _Bool q : 1; /* OK iff C99 */
    };

    but the standard (and K&R) places no constraints on what size is
    actually allocated:

    6.7.2.1 Structure and union specifiers

    10 An implementation may allocate any addressable storage unit large
    enough to hold a bit-field. If enough space remains, a bit-field
    that immediately follows another bit-field in a structure shall be
    packed into adjacent bits of the same unit. If insufficient space
    remains, whether a bit-field that does not fit is put into the next
    unit or overlaps adjacent units is implementation-defined. The
    order of allocation of bit-fields within a unit (high-order to
    low-order or low-order to high-order) is implementation-defined.
    The alignment of the addressable storage unit is unspecified.

    [INCITS/ISO/IEC 9899-1999]

    For that matter a compiler might have a minimum allocation size for any
    struct regardless of what fields are inside it (so a struct containing
    only one byte might still take up 4 or 8 bytes, for instance). As long
    as the fields within a structure as aligned as necessary (and note that
    bitfields may not be aligned even to byte boundaries).

    Chris C
    Chris Croughton, Jul 6, 2005
    #9
  10. On Tue, 05 Jul 2005 17:37:11 -0400, Joe Wright wrote:

    > David Lago wrote:
    >> Abhi wrote:
    >>
    >>>would define a variable of 2-bit length. My doubt is that what would
    >>>the size of this structure then? Would it be sizeof(int) or just 1
    >>>byte?

    >>
    >>
    >> The actual size of the struct depends on your compiler, and which amount
    >> of space it allocates for each word of data. In gcc, it allocates space
    >> in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So
    >>

    > No David, gcc doesn't do that. Try this and tell me what you get..
    >
    > #include <stdio.h>
    >
    > struct c {
    > char c;
    > } c;
    >
    > struct s {
    > short s;
    > } s;


    You are correct that this is not a general structure allocation strategy,
    but the topic here but given the topic of the thread we're probably
    talking about the allocation of bit-fields.

    Lawrence
    Lawrence Kirby, Jul 6, 2005
    #10
  11. Abhi

    Joe Wright Guest

    Lawrence Kirby wrote:
    > On Tue, 05 Jul 2005 17:37:11 -0400, Joe Wright wrote:
    >
    >
    >>David Lago wrote:
    >>
    >>>Abhi wrote:
    >>>
    >>>
    >>>>would define a variable of 2-bit length. My doubt is that what would
    >>>>the size of this structure then? Would it be sizeof(int) or just 1
    >>>>byte?
    >>>
    >>>
    >>>The actual size of the struct depends on your compiler, and which amount
    >>>of space it allocates for each word of data. In gcc, it allocates space
    >>>in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So
    >>>

    >>
    >>No David, gcc doesn't do that. Try this and tell me what you get..
    >>
    >>#include <stdio.h>
    >>
    >>struct c {
    >> char c;
    >>} c;
    >>
    >>struct s {
    >> short s;
    >>} s;

    >
    >
    > You are correct that this is not a general structure allocation strategy,
    > but the topic here but given the topic of the thread we're probably
    > talking about the allocation of bit-fields.
    >
    > Lawrence
    >


    You're right of course. Apologies to David.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 7, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,715
    Timothy Bendfelt
    Jan 19, 2007
  2. Replies:
    9
    Views:
    938
    Juha Nieminen
    Aug 22, 2007
  3. call_me_anything
    Replies:
    4
    Views:
    450
    Pete Becker
    Sep 30, 2007
  4. Mike -- Email Ignored

    Bit Order in Bit Fields

    Mike -- Email Ignored, May 2, 2009, in forum: C++
    Replies:
    0
    Views:
    367
    Mike -- Email Ignored
    May 2, 2009
  5. Jeff.M
    Replies:
    6
    Views:
    162
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page