alignment of structures in c++

Discussion in 'C++' started by Sandeep, Dec 4, 2005.

  1. Sandeep

    Sandeep Guest

    struct x
    {
    int a;
    char b;
    int c;
    char d;
    };

    typedef struct x X;

    int main()
    {
    printf("Sizeof X:%d\n",sizeof(X));
    return 0;
    }

    output of above : 16 .


    Stroustrup says that to optimize space, the members should be ordered
    by size ( largest member first).

    Why do the compilers not optimize it automatically? In other words,
    when would i need the memory layout of the structure to be mainted as
    it is declared.
    Sandeep, Dec 4, 2005
    #1
    1. Advertising

  2. Sandeep wrote:
    > struct x
    > {
    > int a;
    > char b;
    > int c;
    > char d;
    > };
    >
    > typedef struct x X;
    >
    > int main()
    > {
    > printf("Sizeof X:%d\n",sizeof(X));
    > return 0;
    > }
    >
    > output of above : 16 .
    >
    >
    > Stroustrup says that to optimize space, the members should be ordered
    > by size ( largest member first).


    What Stroustrup actually says (TCPL 3rd sec. 5.7) is "You can minimize
    wasted space by simply ordering members by size (largest member first).
    However it is usually best to order members for readability and sort
    them by size only if there is a demonstrated need to optimize." In
    other words, don't optimize early.

    > Why do the compilers not optimize it automatically? In other words,
    > when would i need the memory layout of the structure to be mainted as
    > it is declared.


    Since this a POD struct, it needs to be layout compatible with C. C
    requires that the elements appear in the order listed in the
    declaration. Letting a C++ compiler rearrange the elements would
    therefore break compatibility with C.

    Why does C have that requirement? Ask in a C newsgroup.

    Best regards,

    Tom
    Thomas Tutone, Dec 4, 2005
    #2
    1. Advertising

  3. Sandeep wrote:

    > Why do the compilers not optimize it automatically? In other words,
    > when would i need the memory layout of the structure to be mainted as
    > it is declared.


    Data member variables are initialized in the order in which they are
    declared inside the class. If the compiler is allowed to rearrange the
    data members in a way it wants, the order of construction (as dictated
    by the class writer) would go for a toss - or the compiler would need
    to store that somewhere and refer to that every time the object is
    instantiated - causing an extra overhead.
    Neelesh Bodas, Dec 4, 2005
    #3
  4. Neelesh Bodas wrote:
    > Sandeep wrote:
    >
    >
    >>Why do the compilers not optimize it automatically? In other words,
    >>when would i need the memory layout of the structure to be mainted as
    >>it is declared.

    >
    >
    > Data member variables are initialized in the order in which they are
    > declared inside the class. If the compiler is allowed to rearrange the
    > data members in a way it wants, the order of construction (as dictated
    > by the class writer) would go for a toss - or the compiler would need
    > to store that somewhere and refer to that every time the object is
    > instantiated - causing an extra overhead.


    There is no extra overhead here.

    It's simply compliant with the standard.
    Gianni Mariani, Dec 4, 2005
    #4
  5. Thomas Tutone wrote:
    .....
    >
    > Since this a POD struct, it needs to be layout compatible with C. C
    > requires that the elements appear in the order listed in the
    > declaration. Letting a C++ compiler rearrange the elements would
    > therefore break compatibility with C.
    >
    > Why does C have that requirement? Ask in a C newsgroup.


    I think it's topical here (IMHO).

    The first C++ compiler (and still many compilers) use a C++ front-end to
    a C compiler, so layout rules for data structures were sort of "designed
    into" the standard.

    C has a "poor man's" type of inheritance. If the compiler re-ordered
    struct members it would make it almost impossible to do that. IIRC,
    alignment padding was not an issue in the first incantations of the C
    compiler. The first C compiler I used I don't believe did any alignment
    padding. So to maintain compatability with code written on systems
    where alignment padding was not needed, a rule like the one currently in
    the standard had to be enforced, otherwise a whole bunch of code would
    break.

    So, nothing stops you from ordering the members in your struct in a
    better way. You may be constrained however.

    e.g. (A made-up example use your imagination).

    struct A
    {
    A( B * foo, C * zoo )
    : m_btype( B->ComputeBtype( zoo ) ),
    m_xtype( new X( m_btype ) ),
    m_ytype( new Y( zoo->Fetch( m_btype ) )
    {
    }

    char m_btype;
    X * m_xtype;
    Y * m_ytype;
    char m_code[3];
    };

    In this case, if you move m_btype, your're introducing a bug.
    Gianni Mariani, Dec 4, 2005
    #5
  6. Gianni Mariani wrote:
    > Thomas Tutone wrote:
    > ....
    > >
    > > Since this a POD struct, it needs to be layout compatible with C. C
    > > requires that the elements appear in the order listed in the
    > > declaration. Letting a C++ compiler rearrange the elements would
    > > therefore break compatibility with C.
    > >
    > > Why does C have that requirement? Ask in a C newsgroup.

    >
    > I think it's topical here (IMHO).


    Apologies - I didn't mean to imply otherwise - far be it from me to act
    as the OT police. I did think that since it was a C question, the OP
    might get more knowledgeable answers in a C newsgroup.

    Best regards,

    Tom
    Thomas Tutone, Dec 4, 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. Alfonso Morra
    Replies:
    11
    Views:
    693
    Emmanuel Delahaye
    Sep 24, 2005
  2. Memory alignment in structures

    , Sep 26, 2005, in forum: C Programming
    Replies:
    1
    Views:
    414
    Gordon Burditt
    Sep 26, 2005
  3. structures and alignment issues

    , Jun 14, 2007, in forum: C Programming
    Replies:
    31
    Views:
    778
    Keith Thompson
    Jun 16, 2007
  4. Replies:
    20
    Views:
    675
    Richard
    Aug 10, 2007
  5. mathog

    Data alignment questin, structures

    mathog, Jan 12, 2013, in forum: C Programming
    Replies:
    46
    Views:
    540
    Keith Thompson
    Jan 22, 2013
Loading...

Share This Page