Struct alignment

Discussion in 'C++' started by Hipo, May 25, 2006.

  1. Hipo

    Hipo Guest

    Hi.
    Assuming a struct:

    struct data{
    int, a, b, c, d;
    };

    Can I assume that the four variables from data are stored in memory in
    order given above? Or would it be possible that they look like this in
    memory: a, c, d, b?

    I'm using the Visual Studio 2005 Pro compiler.

    g, Hipo
     
    Hipo, May 25, 2006
    #1
    1. Advertising

  2. Hipo wrote:
    > Assuming a struct:
    >
    > struct data{
    > int, a, b, c, d;
    > };
    >
    > Can I assume that the four variables from data are stored in memory in
    > order given above?


    Yes, that's mandated by the Standard.

    > Or would it be possible that they look like this in
    > memory: a, c, d, b?


    No.

    > I'm using the Visual Studio 2005 Pro compiler.


    Shouldn't matter.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 25, 2006
    #2
    1. Advertising

  3. Hipo

    Hipo Guest

    Victor Bazarov schrieb:
    > Hipo wrote:
    >> Assuming a struct:
    >>
    >> struct data{
    >> int, a, b, c, d;
    >> };
    >>
    >> Can I assume that the four variables from data are stored in memory in
    >> order given above?

    >
    > Yes, that's mandated by the Standard.


    Great, that saves my day :)

    >> I'm using the Visual Studio 2005 Pro compiler.

    >
    > Shouldn't matter.


    Hope so, standard is not always standard (bad translation of a german
    saying) :)

    g, Hipo
     
    Hipo, May 25, 2006
    #3
  4. Hipo

    phlip Guest

    Hipo wrote:

    >>> Can I assume that the four variables from data are stored in memory in
    >>> order given above?

    >>
    >> Yes, that's mandated by the Standard.

    >
    > Great, that saves my day :)


    You can't assume they aren't padded; that's implementation specific.

    (Yes, VC++ probably doesn't pad them.)

    Don't rely on it. You weren't going to treat that struct as binary data,
    were you?

    --
    Phlip
     
    phlip, May 25, 2006
    #4
  5. Hipo

    Tomás Guest

    Hipo posted:

    > Hi.
    > Assuming a struct:
    >
    > struct data{
    > int, a, b, c, d;
    > };
    >
    > Can I assume that the four variables from data are stored in memory in
    > order given above? Or would it be possible that they look like this in
    > memory: a, c, d, b?
    >
    > I'm using the Visual Studio 2005 Pro compiler.
    >
    > g, Hipo



    Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
    members -> Array elements".


    -Tomás
     
    Tomás, May 25, 2006
    #5
  6. Hipo

    Jack Klein Guest

    On Thu, 25 May 2006 22:35:30 GMT, "Tomás" <No.Email@Address> wrote in
    comp.lang.c++:

    > Hipo posted:
    >
    > > Hi.
    > > Assuming a struct:
    > >
    > > struct data{
    > > int, a, b, c, d;
    > > };
    > >
    > > Can I assume that the four variables from data are stored in memory in
    > > order given above? Or would it be possible that they look like this in
    > > memory: a, c, d, b?
    > >
    > > I'm using the Visual Studio 2005 Pro compiler.
    > >
    > > g, Hipo

    >
    >
    > Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
    > members -> Array elements".
    >
    >
    > -Tomás


    No, this question is only about the order, not about the possibility
    of padding between the members.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 26, 2006
    #6
  7. Hipo

    Tomás Guest

    Jack Klein posted:


    > No, this question is only about the order, not about the possibility
    > of padding between the members.


    I realise that... but the order of the members is of no relevance unless
    you try to do something funky like access them with pointers:

    data obj;

    int *p = &obj.a;

    *p = 1; /* We're assuming this is A */

    *++p = 2; /* We're assuming this is B */

    *++p = 3; /* We're assuming this is C */

    *++p = 4; /* We're assuming this is D */


    If you don't try something funky like this, then there's no reason to
    care about the order of the members.

    (Which makes me wonder: Why does C++ even guarantee that the members be
    in order one-after-another in a POD struct? We already don't have a
    guarantee of the absence of padding... so there's no point in
    guaranteeing that they be in order).

    Okay... well... there might be ONE use:

    bool IsAhead( const int * const a, const int * const b )
    {
    return b > a;
    }

    int main()
    {
    data obj;

    IsAhead( &obj.a, &obj.b );
    }


    -Tomás
     
    Tomás, May 26, 2006
    #7
    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. Bryan Parkoff
    Replies:
    0
    Views:
    452
    Bryan Parkoff
    Apr 2, 2005
  2. Eric G.

    Re: Struct Alignment

    Eric G., Aug 3, 2003, in forum: C Programming
    Replies:
    2
    Views:
    365
    Richard Heathfield
    Aug 4, 2003
  3. Jack Klein

    Re: Struct Alignment

    Jack Klein, Aug 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    415
    Jack Klein
    Aug 4, 2003
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,440
    Chris Fogelklou
    Apr 20, 2004
  5. S.Tobias

    struct and union alignment

    S.Tobias, Sep 23, 2004, in forum: C Programming
    Replies:
    67
    Views:
    1,398
    Dave Thompson
    Oct 4, 2004
Loading...

Share This Page