Structure padding

Discussion in 'C Programming' started by Rohit kumar Chandel, Sep 26, 2007.

  1. Hi All,

    Please let me know how to find in a structure whether compiler has used
    padding or not.

    Regards
    Rohit
    Rohit kumar Chandel, Sep 26, 2007
    #1
    1. Advertising

  2. Rohit kumar Chandel said:

    > Hi All,
    >
    > Please let me know how to find in a structure whether compiler has used
    > padding or not.


    If you add together the sizes of all the components of a struct, and
    subtract it from the size of the struct, the remainder is the number of
    bytes of padding. If this value is 0, there is no padding.

    Apply recursively, depth-first, if the structure contains any structures
    (there's no rule that says an implementation's padding strategy must be
    all or nothing).

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 26, 2007
    #2
    1. Advertising

  3. Thanks for the info.
    Another question. I have read somewhere concept of byte boundaries in
    context of Structure padding. But could get nothing out of it since no
    explaination was provided as what is byte boundary.

    Can anybody comment what 1byte, 2byte, 4 byte boundary mean in this context.
    Suppose my structure starts at address 1000 and occupies 13 bytes. Then I
    want to know in this example what are 1 byte, 2byte, 4 byte and 16 byte
    boundaries.

    Thanks and Regards
    Rohit

    "Richard Heathfield" <> wrote in message
    news:D...
    > Rohit kumar Chandel said:
    >
    > > Hi All,
    > >
    > > Please let me know how to find in a structure whether compiler has used
    > > padding or not.

    >
    > If you add together the sizes of all the components of a struct, and
    > subtract it from the size of the struct, the remainder is the number of
    > bytes of padding. If this value is 0, there is no padding.
    >
    > Apply recursively, depth-first, if the structure contains any structures
    > (there's no rule that says an implementation's padding strategy must be
    > all or nothing).
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999
    Rohit kumar Chandel, Sep 26, 2007
    #3
  4. Rohit kumar Chandel

    Mark Bluemel Guest

    Rohit kumar Chandel wrote:

    [Top-posting and failure to remove sigs corrected]

    > "Richard Heathfield" <> wrote in message
    > news:D...
    >> Rohit kumar Chandel said:
    >>
    >>> Hi All,
    >>>
    >>> Please let me know how to find in a structure whether compiler has used
    >>> padding or not.

    >> If you add together the sizes of all the components of a struct, and
    >> subtract it from the size of the struct, the remainder is the number of
    >> bytes of padding. If this value is 0, there is no padding.
    >>
    >> Apply recursively, depth-first, if the structure contains any structures
    >> (there's no rule that says an implementation's padding strategy must be
    >> all or nothing).


    > Thanks for the info.
    > Another question. I have read somewhere concept of byte boundaries in
    > context of Structure padding. But could get nothing out of it since no
    > explaination was provided as what is byte boundary.


    Although a system may allow byte-level addressing, it's unlikely that
    data types can start at arbitrary byte addresses. So data types have to
    be aligned at addresses which meet specific requirements.

    For example, on the PowerPC systems I've worked on, 32-bit data items
    (e.g. ints on that system) had to be at addresses which were multiples
    of 4-bytes while 64-bit data items (longs, doubles) had to be at 8-byte
    boundaries... As a structure could start with any data type, structures
    were also 8-byte aligned, as was memory returned by malloc().

    In this model a structure of the form :-

    struct fred {
    char a;
    long b;
    };

    would have 7-bytes of padding between a and b.
    Mark Bluemel, Sep 26, 2007
    #4
  5. On Sep 26, 10:28 am, "Rohit kumar Chandel"
    <> wrote:
    > Hi All,
    >
    > Please let me know how to find in a structure whether compiler has used
    > padding or not.
    >
    > Regards
    > Rohit


    Try 'offsetof'. It will help you in understanding the padding ,
    alignment.

    Karthik Balaguru
    karthikbalaguru, Sep 26, 2007
    #5
  6. Rohit kumar Chandel

    Spade Guest

    On Sep 26, 1:42 pm, "Rohit kumar Chandel"
    <> wrote:
    > Thanks for the info.
    > Another question. I have read somewhere concept of byte boundaries in
    > context of Structure padding. But could get nothing out of it since no
    > explaination was provided as what is byte boundary.
    >
    > Can anybody comment what 1byte, 2byte, 4 byte boundary mean in this context.
    > Suppose my structure starts at address 1000 and occupies 13 bytes. Then I
    > want to know in this example what are 1 byte, 2byte, 4 byte and 16 byte
    > boundaries.


    You can find an explanation for byte boundaries here:
    http://prokutfaq.byethost15.com/StructurePadding
    Spade, Sep 26, 2007
    #6
  7. Rohit kumar Chandel

    Guest

    On Sep 26, 1:42 pm, "Rohit kumar Chandel"
    <> wrote:
    > Thanks for the info.
    > Another question. I have read somewhere concept of byte boundaries in
    > context of Structure padding. But could get nothing out of it since no
    > explaination was provided as what is byte boundary.
    >
    > Can anybody comment what 1byte, 2byte, 4 byte boundary mean in this context.
    > Suppose my structure starts at address 1000 and occupies 13 bytes. Then I
    > want to know in this example what are 1 byte, 2byte, 4 byte and 16 byte
    > boundaries.
    >
    > Thanks and Regards
    > Rohit
    >
    > "Richard Heathfield" <> wrote in message
    >
    > news:D...
    >
    >
    >
    > > Rohit kumar Chandel said:

    >
    > > > Hi All,

    >
    > > > Please let me know how to find in a structure whether compiler has used
    > > > padding or not.

    >
    > > If you add together the sizes of all the components of a struct, and
    > > subtract it from the size of the struct, the remainder is the number of
    > > bytes of padding. If this value is 0, there is no padding.

    >
    > > Apply recursively, depth-first, if the structure contains any structures
    > > (there's no rule that says an implementation's padding strategy must be
    > > all or nothing).

    >
    > > --
    > > Richard Heathfield <http://www.cpax.org.uk>
    > > Email: -http://www. +rjh@
    > > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > > "Usenet is a strange place" - dmr 29 July 1999- Hide quoted text -

    >
    > - Show quoted text -
    , Sep 26, 2007
    #7
  8. Mark Bluemel wrote:
    > For example, on the PowerPC systems I've worked on, 32-bit data items
    > (e.g. ints on that system) had to be at addresses which were multiples
    > of 4-bytes while 64-bit data items (longs, doubles) had to be at 8-byte
    > boundaries... As a structure could start with any data type, structures
    > were also 8-byte aligned, as was memory returned by malloc().
    >
    > In this model a structure of the form :-
    >
    > struct fred {
    > char a;
    > long b;
    > };
    >
    > would have 7-bytes of padding between a and b.


    Does padding tend to vary between different compilers on the same platform?

    I'm thinking of the situation where struct fred is part of the public
    API for some library. If clients of the library want to be able to
    access the members of fred, they need to be sure that they've been
    compiled using the same padding as the library.

    I know that in general opaque structures are a good idea, but there are
    exceptions where having access functions for everything really isn't
    desirable.

    Regards,

    Chris
    Christopher Key, Sep 27, 2007
    #8
  9. On Sep 27, 9:46 pm, Christopher Key <> wrote:
    > Mark Bluemel wrote:
    > > For example, on the PowerPC systems I've worked on, 32-bit data items
    > > (e.g. ints on that system) had to be at addresses which were multiples
    > > of 4-bytes while 64-bit data items (longs, doubles) had to be at 8-byte
    > > boundaries... As a structure could start with any data type, structures
    > > were also 8-byte aligned, as was memory returned by malloc().

    >
    > > In this model a structure of the form :-

    >
    > > struct fred {
    > > char a;
    > > long b;
    > > };

    >
    > > would have 7-bytes of padding between a and b.

    >
    > Does padding tend to vary between different compilers on the same platform?


    Padding is done by compiler based on the Boundary alignment of the
    processor.
    Padding contain some undefined data. The compiler is never going to
    do
    anything with them and so they could be anything(Padded data will not
    be sequence of
    zeros or one)

    >
    > I'm thinking of the situation where struct fred is part of the public
    > API for some library. If clients of the library want to be able to
    > access the members of fred, they need to be sure that they've been
    > compiled using the same padding as the library.


    Obviously, some libraries will throw the respective exceptions or
    carry a note about this.
    And probably, that is one of the reason, different set of libraries
    are present for
    different architecture w.r.t differnt compilers. :):)

    Karthik Balaguru
    karthikbalaguru, Sep 27, 2007
    #9
  10. In article <>,
    karthikbalaguru <> wrote:
    >On Sep 27, 9:46 pm, Christopher Key <> wrote:


    >> Does padding tend to vary between different compilers on the same platform?


    >Padding is done by compiler based on the Boundary alignment of the
    >processor.


    Not necessarily. Padding can be done for efficiency instead of
    based upon instruction set necessity. Compiler optimizations might
    even pad so as to avoid bad cache behaviour based upon the way
    the program uses the structure.
    --
    Prototypes are supertypes of their clones. -- maplesoft
    Walter Roberson, Sep 27, 2007
    #10
  11. Walter Roberson wrote:
    > In article <>,
    > karthikbalaguru <> wrote:
    >> On Sep 27, 9:46 pm, Christopher Key <> wrote:

    >
    >>> Does padding tend to vary between different compilers on the same platform?

    >
    >> Padding is done by compiler based on the Boundary alignment of the
    >> processor.

    >
    > Not necessarily. Padding can be done for efficiency instead of
    > based upon instruction set necessity. Compiler optimizations might
    > even pad so as to avoid bad cache behaviour based upon the way
    > the program uses the structure.


    Thanks guys. Hopefully all code using the API will typically be
    compiled using the same compiler as was used for the API, but I'll
    remember this as a potential source of problems.

    Chris
    Christopher Key, Sep 28, 2007
    #11
  12. Rohit kumar Chandel

    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
    #12
    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,575
    Ralmin
    Jun 22, 2004
  2. phoenix

    structure padding

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

    structure padding

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

    Size of a structure : Structure Padding

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

Share This Page