structure padding

Discussion in 'C Programming' started by Stephen Mayes, May 20, 2005.

  1. I this helloworld portable? I am vaguely aware of something called
    "structure padding" and wonder if it could affect this program since the
    struct only contains chars.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main (void)
    {
    struct
    {
    unsigned char var1[5];
    unsigned char var2[6];
    unsigned char var3[2];
    } var =
    {
    {'H', 'e', 'l', 'l', 'o'},
    {' ', 'W', 'o', 'r', 'l', 'd'},
    {'!', '\n'}
    };
    char varcopy[sizeof(var)];
    size_t i;

    memcpy (varcopy, &var, sizeof(varcopy));

    for (i = 0; i < sizeof(varcopy); i++)
    putchar (varcopy);

    return EXIT_SUCCESS;
    }
    Stephen Mayes, May 20, 2005
    #1
    1. Advertising

  2. Stephen Mayes wrote:
    > I this helloworld portable? I am vaguely aware of something called
    > "structure padding" and wonder if it could affect this program since the
    > struct only contains chars.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main (void)
    > {
    > struct
    > {
    > unsigned char var1[5];
    > unsigned char var2[6];
    > unsigned char var3[2];
    > } var =
    > {
    > {'H', 'e', 'l', 'l', 'o'},
    > {' ', 'W', 'o', 'r', 'l', 'd'},
    > {'!', '\n'}
    > };
    > char varcopy[sizeof(var)];
    > size_t i;
    >
    > memcpy (varcopy, &var, sizeof(varcopy));
    >
    > for (i = 0; i < sizeof(varcopy); i++)
    > putchar (varcopy);
    >
    > return EXIT_SUCCESS;
    > }
    >
    >


    The code is portable. However, the output of
    your program may differ depending on platforms
    and compiler versions.

    The compiler is allowed to insert padding bytes
    between members of a class, union or structure.
    The value of the padding bytes is undefined.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, May 20, 2005
    #2
    1. Advertising

  3. Stephen Mayes

    SM Ryan Guest

    "Stephen Mayes" <> wrote:
    # I this helloworld portable? I am vaguely aware of something called
    # "structure padding" and wonder if it could affect this program since the
    # struct only contains chars.

    Some CPU prefer to load and store some types of operands are aligned to
    particular byte boundaries. For example ints are often stored word boundaries,
    at addresses that are a multiple of 4. Since struct fields are required to
    be stored in order, packing them in tightly can put a field at an awkard
    boundary. So the compiler can insert unused bytes between fields to
    move a later field back to the next desired boundary.

    The code will work in the sense that sizeof accounts for padding in the
    struct, and memcpy copies padding bytes in sequence with field bytes.
    Whether this struct has padding is up to the compiler. It's possible
    that it would have 3 padding bytes at the end to bring the size to 16.

    # #include <stdio.h>
    # #include <stdlib.h>
    # #include <string.h>
    #
    # int main (void)
    # {
    # struct
    # {
    # unsigned char var1[5];
    # unsigned char var2[6];
    # unsigned char var3[2];
    # } var =
    # {
    # {'H', 'e', 'l', 'l', 'o'},
    # {' ', 'W', 'o', 'r', 'l', 'd'},
    # {'!', '\n'}
    # };
    # char varcopy[sizeof(var)];
    # size_t i;
    #
    # memcpy (varcopy, &var, sizeof(varcopy));
    #
    # for (i = 0; i < sizeof(varcopy); i++)
    # putchar (varcopy);
    #
    # return EXIT_SUCCESS;
    # }
    #

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    So basically, you just trace.
    SM Ryan, May 20, 2005
    #3
  4. EventHelix.com, May 23, 2005
    #4
  5. Stephen Mayes

    Eric Sosman Guest

    EventHelix.com wrote:
    > The following article about structure alignment should help:
    >
    > http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm


    Note that the article is not about C, but about "C as
    it might be implemented on a certain class of processors."
    C is most certainly implemented on processors that do not
    fit the article's model, so be sure to read the article as
    an example, not as a definition.

    Also, the example titled "Actual Structure Definition
    Used By the Compiler" is wrong: the `pad3' field is larger
    than the article's assumptions say it should be.

    --
    Eric Sosman, May 23, 2005
    #5
  6. On Fri, 20 May 2005 10:17:46 -0400, "Stephen Mayes"
    <> wrote:

    > I this helloworld portable? I am vaguely aware of something called
    > "structure padding" and wonder if it could affect this program since the
    > struct only contains chars.

    <snip: struct var containing several initialized char[] fields>
    > char varcopy[sizeof(var)];
    > size_t i;
    >
    > memcpy (varcopy, &var, sizeof(varcopy));
    >
    > for (i = 0; i < sizeof(varcopy); i++)
    > putchar (varcopy);


    In practice portable but not quite in theory.

    An implementation (compiler) is allowed to add padding after any
    member of a struct for any reason, including "today's the third
    Tuesday". In practice the only _good_ reason is for alignment, and no
    char type in C can require nontrivial alignment, since a pointer to
    (flavored) char must be able to access any byte of usable memory.

    In the unlikely event there is padding, it is safely copied by memcpy
    which accesses memory as unsigned char's. But the implementation
    chooses whether to treat plain char like signed or unsigned, and if it
    chooses signed that is permitted to have trap representations and the
    padding could be such, and if so fetching it (for output) is Undefined
    Behavior; but again there is probably no good reason any actual
    implementation will screw up.

    BTW you could instead just do one call:
    fwrite (stdout, varcopy, sizeof varcopy, 1);
    Or if you prefer ... varcopy, 1, sizeof varcopy.
    Or indeed fwrite from var without copying at all.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, May 31, 2005
    #6
    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,571
    Ralmin
    Jun 22, 2004
  2. phoenix

    structure padding

    phoenix, Mar 11, 2005, in forum: C Programming
    Replies:
    1
    Views:
    405
    Peter Nilsson
    Mar 11, 2005
  3. Replies:
    6
    Views:
    447
    Flash Gordon
    May 18, 2005
  4. ramu

    structure padding

    ramu, Feb 2, 2006, in forum: C Programming
    Replies:
    28
    Views:
    889
    Jordan Abel
    Feb 5, 2006
  5. Kislay

    Size of a structure : Structure Padding

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

Share This Page