struct memory padding

Discussion in 'C++' started by simonp@nospam.com, Jan 22, 2007.

  1. Guest

    I'm taking an intro course on C++, and our teacher is not being
    clear on how stuct memory padding is determined.

    If the memory used by all components defined inside a struct
    falls between a certain gradient, a struct "rounds up" to the
    nearest multiple of the gradient.

    This teacher is somewhat erratic and has said on subsequent days
    that first the padding is rounded up to nearest multiple of 4,
    and then the nearest power of 2.

    I reading on my own, it looks like the padding of the struct is
    determined by the word size fo the platform, which I assume means
    multiples of 16.

    Could someone well-versed in C++ please point out how this is
    done?

    Cheers,
    Simon

    --
    Stupendous Tales
    www.stupendoustales.com
    Speculative Fiction, Pulp Dreams
     
    , Jan 22, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I'm taking an intro course on C++, and our teacher is not being
    > clear on how stuct memory padding is determined.
    >
    > If the memory used by all components defined inside a struct
    > falls between a certain gradient, a struct "rounds up" to the
    > nearest multiple of the gradient.
    >
    > This teacher is somewhat erratic and has said on subsequent days
    > that first the padding is rounded up to nearest multiple of 4,
    > and then the nearest power of 2.
    >
    > I reading on my own, it looks like the padding of the struct is
    > determined by the word size fo the platform, which I assume means
    > multiples of 16.
    >
    > Could someone well-versed in C++ please point out how this is
    > done?
    >

    While not defined by the standard, padding tends to be to the natural
    alignment of the processor So on a 32 bit processor, the struct

    struct X {
    char a;
    int b;
    };

    will have a size of 8 with three padding bytes after 'a'.

    Some platforms support packing to eliminate padding, but this is
    non-standard and impossible on some processors.

    --
    Ian Collins.
     
    Ian Collins, Jan 22, 2007
    #2
    1. Advertising

  3. Yahooooooooo Guest

    Hope this helps you
    http://www.thescripts.com/forum/thread543879.html


    wrote:
    > I'm taking an intro course on C++, and our teacher is not being
    > clear on how stuct memory padding is determined.
    >
    > If the memory used by all components defined inside a struct
    > falls between a certain gradient, a struct "rounds up" to the
    > nearest multiple of the gradient.
    >
    > This teacher is somewhat erratic and has said on subsequent days
    > that first the padding is rounded up to nearest multiple of 4,
    > and then the nearest power of 2.
    >
    > I reading on my own, it looks like the padding of the struct is
    > determined by the word size fo the platform, which I assume means
    > multiples of 16.
    >
    > Could someone well-versed in C++ please point out how this is
    > done?
    >
    > Cheers,
    > Simon
    >
    > --
    > Stupendous Tales
    > www.stupendoustales.com
    > Speculative Fiction, Pulp Dreams
     
    Yahooooooooo, Jan 22, 2007
    #3
  4. Guest

    Thanks so much. Padding of the struct will bring it up to the
    next multiple of the word size, eg. 32bit/4byte

    Cheers,
    Simon

    --
    Stupendous Tales
    www.stupendoustales.com
    Speculative Fiction, Pulp Dreams
     
    , Jan 22, 2007
    #4
  5. wrote:
    > I'm taking an intro course on C++, and our teacher is not being
    > clear on how stuct memory padding is determined.
    >
    > If the memory used by all components defined inside a struct
    > falls between a certain gradient, a struct "rounds up" to the
    > nearest multiple of the gradient.
    >
    > This teacher is somewhat erratic and has said on subsequent days
    > that first the padding is rounded up to nearest multiple of 4,
    > and then the nearest power of 2.
    >
    > I reading on my own, it looks like the padding of the struct is
    > determined by the word size fo the platform, which I assume means
    > multiples of 16.
    >
    > Could someone well-versed in C++ please point out how this is
    > done?


    While padding is specifically allowed in the
    C++ standard, it is not specified. It in implementation dependant.

    In general, however, the implementations of padding is fairly uniform.

    Each member of a struct has an "alignment" which is implementation
    defined. The alignment of various types is sometimes mandatory (like in
    RISC processors - MIPS, PowerPC, SPARC) and sometimes and optimization
    (e.g. IA32, AMD64).

    The resulting alignment of a struct is the alignment of the member(s)
    with the largest alignment.

    e.g.

    struct X { char a; char b; }; // has alignment of char

    struct Y { char a; int b; short c; }; // has alignment of int

    Padding is added between members to guarentee that the offset of all
    members in the struct falls on a multiple of the alignment.

    e.g.
    if alignment of :

    char is 1
    long long is 8

    then

    struct Z
    {
    char a;
    // padding added here (7 bytes)
    long long b; // offset is 8
    };

    sizeof Z is 16
    alignment of Z is 8

    .....

    While that is the basic implementation, the actual alignments for each
    type varies between platforms and sometimes on different "architectures"
    or ABI's on each platform. For example, on MIPS o32, alignment of
    double is 4 while on MIPS n32, alignment of double is 8 (IIRC).

    It's really not all that hard.
     
    Gianni Mariani, Jan 22, 2007
    #5
  6. Ian Collins Guest

    Ian Collins, Jan 22, 2007
    #6
  7. John Carson Guest

    <> wrote in message news:ep1ok2$h9a$
    > I'm taking an intro course on C++, and our teacher is not being
    > clear on how stuct memory padding is determined.
    >
    > If the memory used by all components defined inside a struct
    > falls between a certain gradient, a struct "rounds up" to the
    > nearest multiple of the gradient.
    >
    > This teacher is somewhat erratic and has said on subsequent days
    > that first the padding is rounded up to nearest multiple of 4,
    > and then the nearest power of 2.
    >
    > I reading on my own, it looks like the padding of the struct is
    > determined by the word size fo the platform, which I assume means
    > multiples of 16.
    >
    > Could someone well-versed in C++ please point out how this is
    > done?


    The C++ standard makes no rules on this. It depends on the platform and is
    sometimes controllable using compiler switches. It can also vary with the
    order of declaration of variables.

    Using VC++ on Windows, you can eliminate all padding if you want. By
    default, a struct is rounded up to a multiple of its largest data member,
    but still leaves a possible role for the order of declaration, e.g.,

    struct X1
    {
    char ch1;
    int x1;
    char ch2;
    int x2;
    };

    has a size of 16. However, the following struct, with the same members in a
    different order, has a size of 12.

    struct X2
    {
    char ch1;
    char ch2;
    int x1;
    int x2;
    };

    The reason that X1 is larger is because the two ints are aligned on 4 byte
    boundaries, so there are 3 bytes after each char. With X2, by contrast, you
    only need 2 bytes after ch2 to achieve alignment of the ints on 4 byte
    boundaries.

    Read up the documentation for the compiler you are interested in to see how
    it is handled.

    --
    John Carson
     
    John Carson, Jan 22, 2007
    #7
  8. <> wrote in message
    news:ep1ok2$h9a$...
    : I'm taking an intro course on C++, and our teacher is not being
    : clear on how stuct memory padding is determined.
    :
    : If the memory used by all components defined inside a struct
    : falls between a certain gradient, a struct "rounds up" to the
    : nearest multiple of the gradient.
    :
    : This teacher is somewhat erratic and has said on subsequent days
    : that first the padding is rounded up to nearest multiple of 4,
    : and then the nearest power of 2.
    :
    : I reading on my own, it looks like the padding of the struct is
    : determined by the word size fo the platform, which I assume means
    : multiples of 16.
    :
    : Could someone well-versed in C++ please point out how this is
    : done?

    [ keep in mind that everything related to padding is architecture-
    dependent, and not something specified in the C++ standard ]

    Padding is dependent on the alignment requirements of the data
    members (or fields) of the struct.
    For example:
    struct FiveChars { char a,b,c,d,e; };
    often does not have any padding (neither between members,
    nor at the end of the struct): sizeof(FiveChars)==5


    Here's how I would summarize things:

    The alignment requirement of a data member normally is a
    power of two, usually up to the largest word size of the
    target architecture.
    For example: char<->1 short<->2 long<->4 long long<-> 4 or 8 ?
    (but on a 16-bit architecture, 2 bytes might be the largest alignment
    requirement, applying to all primitive types except (signed/u)char ).

    Each data member is preceded by the padding required to align
    itself properly relative to the beginning of the struct.

    The size of the whole struct is padded so that it is a multiple
    of the largest alignment requirement of its data fields.


    I hope this helps --Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, Jan 22, 2007
    #8
  9. Greg Guest

    wrote:
    > Thanks so much. Padding of the struct will bring it up to the
    > next multiple of the word size, eg. 32bit/4byte


    There is no standard rule that will predict how a C++ compiler will
    distribute padding bytes (if any) within a particular struct.

    In fact, it would be a mistake to think that there is only alignment
    convention per machine architecture. On the contrary, alignment
    conventions tend to accumulate over time. Old ones never really go away
    - because as soon as some struct with a particular alignment convention
    is saved to a disk for the first time - then that alignment convention
    will effectively be around forever. For example, C++ compilers on the
    Macintosh recognized three different alignment conventions that exist
    on that platform (68K, Power, "natural"). And most C++ compilers allow
    a programmer to specify the size and location of a struct's padding
    bytes themselves, just in case the conventional schemes are not enough.

    Greg
     
    Greg, Jan 22, 2007
    #9
  10. peter koch Guest

    wrote:
    > Thanks so much. Padding of the struct will bring it up to the
    > next multiple of the word size, eg. 32bit/4byte
    >
    > Cheers,
    > Simon
    >
    > --
    > Stupendous Tales
    > www.stupendoustales.com
    > Speculative Fiction, Pulp Dreams

    The statements in the link given in a previous post were not entirely
    accurate. Basically, padding occurs because the underlying CPU either
    requires or prefers a given alignment when reading/writing fundamental
    types. This is not necesarrily the same size as the word size. One
    example is the newer x86 processors where an optimal reading of doubles
    requires the double to be aligned on an 8 byte boundary. Thus
    struct s { char c; double d; } will most likely occupy 16 bytes.

    /Peter
     
    peter koch, Jan 22, 2007
    #10
  11. peter koch Guest

    peter koch wrote:
    [snip]
    > The statements in the link given in a previous post were not entirely
    > accurate.

    [snip]
    And some very good explanations have been given elsewhere in this
    thread.

    /Peter
     
    peter koch, Jan 22, 2007
    #11
  12. Guest

    peter koch <> wrote:
    >
    > wrote:
    >> Thanks so much. Padding of the struct will bring it up to the
    >> next multiple of the word size, eg. 32bit/4byte
    >>
    >> Cheers,
    >> Simon
    >>
    >> --
    >> Stupendous Tales
    >> www.stupendoustales.com
    >> Speculative Fiction, Pulp Dreams

    > The statements in the link given in a previous post were not entirely
    > accurate. Basically, padding occurs because the underlying CPU either
    > requires or prefers a given alignment when reading/writing fundamental
    > types. This is not necesarrily the same size as the word size. One
    > example is the newer x86 processors where an optimal reading of doubles
    > requires the double to be aligned on an 8 byte boundary. Thus
    > struct s { char c; double d; } will most likely occupy 16 bytes.
    >
    > /Peter
    >


    My teacher has given me no background such as this.

    This is the 2nd course in the 2 course intro to programming. The
    1st course used to be C++, but now it is Java, so the low level
    programming techniques that used to be covered in the 1st course
    are lost. But the teacher has not adjusted the 2nd course
    appropriately. Maddening. Though I guess its always better to
    learn on your own.

    Cheers
    Simon

    --
    Stupendous Tales
    www.stupendoustales.com
    Speculative Fiction, Pulp Dreams
     
    , Jan 22, 2007
    #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. Chris Fogelklou
    Replies:
    36
    Views:
    1,393
    Chris Fogelklou
    Apr 20, 2004
  2. edware

    struct padding

    edware, Apr 25, 2006, in forum: C Programming
    Replies:
    9
    Views:
    437
    Ed Vogel
    Apr 27, 2006
  3. Hallvard B Furuseth

    Padding bits and struct assignment

    Hallvard B Furuseth, Dec 27, 2006, in forum: C Programming
    Replies:
    5
    Views:
    656
    Eric Sosman
    Dec 27, 2006
  4. vikas talwar

    struct padding ???

    vikas talwar, May 31, 2008, in forum: C Programming
    Replies:
    3
    Views:
    453
    Keith Thompson
    May 31, 2008
  5. Replies:
    13
    Views:
    361
    glen herrmannsfeldt
    May 5, 2013
Loading...

Share This Page