structure padding

Discussion in 'C Programming' started by fdmfdmfdm@gmail.com, Feb 7, 2007.

  1. Guest

    code like:

    struct pid_tag{
    unsigned int inactive : 1;
    unsigned int : 1; /* 1 bit of padding */
    unsigned int refcount : 6;
    unsigned int : 0; /* pad to next word boundary */
    short pid_id;
    struct pid_tag *link;
    }


    My questions are:
    1. What is this 1 bit padding? Does it mean that pad 1 bit after
    integer inactive? Will this 1 bit pad have the same effect as the 0
    pad below?
    2. What is "pad to next word boundary"? I guess after this 0 bit
    applies, the compiler will align the structure into a word boudary
    according to the machine?
    3. Are the word boundary alignment stops at bit-field, i.e., short int
    pid_id and struct pid_tag *link are not aligned?

    Thank you.
     
    , Feb 7, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    >code like:


    >struct pid_tag{
    > unsigned int inactive : 1;
    > unsigned int : 1; /* 1 bit of padding */
    > unsigned int refcount : 6;
    > unsigned int : 0; /* pad to next word boundary */
    > short pid_id;
    > struct pid_tag *link;
    >}


    >My questions are:
    >1. What is this 1 bit padding? Does it mean that pad 1 bit after
    >integer inactive?


    The order of the bits within a word is not defined, so the
    bitfield named inactive could end up being either the first or
    the last bit in the word. All we can say with certainty is that
    the one bit of padding will be adjacent to the bit used for
    inactive, and that inactive will be at one end of the word or
    the other. We cannot say whether the padding bit will be
    "before" or "after" the other bit, because no direction is
    defined for bitfields.

    >Will this 1 bit pad have the same effect as the 0
    >pad below?


    No.

    >2. What is "pad to next word boundary"? I guess after this 0 bit
    >applies, the compiler will align the structure into a word boudary
    >according to the machine?


    A bit width of 0 requires that the compiler proceed to the next
    (implementation-defined) word boundary and not place more bitfields
    in the previous word even if they would fit there. There is
    an automatic alignment to an appropriate boundary anyhow as soon
    as you finish the run of bitfields, so the :0 is not going to have
    any effect in that particular context. If there had been another
    bitfield after the :0 then there would have been a potential difference
    compared to there not being a :0 .

    If a short int is routinely 8-bit aligned on an implementation,
    then at the location of the :0 the short that comes next would already
    be at the right position for alignment and the :0 wouldn't have
    any effect. If a short int is not 8-bit aligned on the implementation,
    then the :0 would tell it to proceed to the alignment point for
    the short int pid_id -- which is exactly the same thing that would
    happen in this case if the :0 were not there. :0 only matters in
    the middle of lists of bitfields.

    >3. Are the word boundary alignment stops at bit-field, i.e., short int
    >pid_id and struct pid_tag *link are not aligned?


    No, pid_id and link will always be aligned (unless compiler-specific
    mechanisms such as "#pragma pack" are present to force non-alignment.)

    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Feb 7, 2007
    #2
    1. Advertising

  3. clinuxpro

    Joined:
    Jul 13, 2011
    Messages:
    4
    Structure padding is done by the compilers and this depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it.
    http://clinuxpro.com/structure-padding
     
    clinuxpro, Jul 13, 2011
    #3
    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. Amarendra

    Structure padding.

    Amarendra, Jun 21, 2004, in forum: C Programming
    Replies:
    13
    Views:
    9,621
    Ralmin
    Jun 22, 2004
  2. phoenix

    structure padding

    phoenix, Mar 11, 2005, in forum: C Programming
    Replies:
    1
    Views:
    427
    Peter Nilsson
    Mar 11, 2005
  3. Replies:
    6
    Views:
    483
    Flash Gordon
    May 18, 2005
  4. Stephen Mayes

    structure padding

    Stephen Mayes, May 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    462
    Dave Thompson
    May 31, 2005
  5. Kislay

    Size of a structure : Structure Padding

    Kislay, Oct 1, 2007, in forum: C Programming
    Replies:
    15
    Views:
    988
    clinuxpro
    Jul 13, 2011
Loading...

Share This Page