ALIGNMENT REVISITED 2

Discussion in 'C++' started by puzzlecracker, Jan 19, 2005.


  1. > 2)
    >>struct B
    >>{
    >> int i;
    >>short int si;
    >> char c;
    >> char d;
    >> int j;
    >>};


    32 bit machine
    --------------
    1st block: i+pad = 4 bytes
    2rd block: s1+pad = 4 bytes
    3rd block" c+pad = 4 bytes
    4th block: d+ pad = 4 bytes
    5th block: j+pad = 4 bytes
    ---------------------------------------
    TOTAL: 20 bytes


    64 bit Machine
    ----------------
    1st block: i + pad = 8 byte
    2nd block: si + pad = 8 byte
    3rd block: c + pad = 8 byte
    4ht block: d + pad = 8 byte
    5ht block: j + pad = 8 byte


    ---------------------------------
    TOTAL: = 40 bytes



    Why would it be differnt from the previous example?


    would explain how to use sizeof to determine that as well?
    what is POD and ALIGNED ADDRESS?




    what is POD?
    Thanks
    puzzlecracker, Jan 19, 2005
    #1
    1. Advertising

  2. puzzlecracker wrote:
    > [...]
    > what is POD?


    Try to learn to use groups.google.com to look up answers that
    have already been given multiple times.
    Victor Bazarov, Jan 19, 2005
    #2
    1. Advertising

  3. EventHelix.com, Jan 21, 2005
    #3
  4. "EventHelix.com" <> wrote in message
    news:...
    > The following article should help:
    >
    > http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm


    Interesting. A dumb question: is there anything in standards that prohibits
    compiler from rearranging the order of variables declared in a struct or
    class? I have worked on compilers that rearranged variables to avoid padding
    (old S/360). It wouldn't seem prudent, but perhaps there are compiler
    switches for this, or not.
    --
    Gary
    Gary Labowitz, Jan 21, 2005
    #4
  5. Gary Labowitz wrote:
    > "EventHelix.com" <> wrote in message
    > news:...
    >
    >>The following article should help:
    >>
    >>http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    >
    >
    > Interesting. A dumb question: is there anything in standards that prohibits
    > compiler from rearranging the order of variables declared in a struct or
    > class? I have worked on compilers that rearranged variables to avoid padding
    > (old S/360). It wouldn't seem prudent, but perhaps there are compiler
    > switches for this, or not.


    Yes, there is. 9.2/12: "Nonstatic data members of a (non-union) class
    declared without an intervening access-specifier are allocated so that
    later members have higher addresses within a class object."

    V
    Victor Bazarov, Jan 21, 2005
    #5
  6. "Victor Bazarov" <> wrote in message
    news:Rp9Id.37366$01.us.to.verio.net...
    > Gary Labowitz wrote:
    > > "EventHelix.com" <> wrote in message
    > > news:...
    > >
    > >>The following article should help:
    > >>
    > >>http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    > >
    > >
    > > Interesting. A dumb question: is there anything in standards that

    prohibits
    > > compiler from rearranging the order of variables declared in a struct or
    > > class? I have worked on compilers that rearranged variables to avoid

    padding
    > > (old S/360). It wouldn't seem prudent, but perhaps there are compiler
    > > switches for this, or not.

    >
    > Yes, there is. 9.2/12: "Nonstatic data members of a (non-union) class
    > declared without an intervening access-specifier are allocated so that
    > later members have higher addresses within a class object."


    Thank you. Then &b-&a makes sense for b declared after a. And sizeof(a)
    would equal (&b-&a) if no padding were added?
    --
    Gary
    Gary Labowitz, Jan 21, 2005
    #6
  7. Gary Labowitz wrote:
    > "Victor Bazarov" <> wrote in message
    > news:Rp9Id.37366$01.us.to.verio.net...
    >
    >>Gary Labowitz wrote:
    >>[..] A dumb question: is there anything in standards that

    >
    > prohibits
    >
    >>>compiler from rearranging the order of variables declared in a struct or
    >>>class? I have worked on compilers that rearranged variables to avoid

    >
    > padding
    >
    >>>(old S/360). It wouldn't seem prudent, but perhaps there are compiler
    >>>switches for this, or not.

    >>
    >>Yes, there is. 9.2/12: "Nonstatic data members of a (non-union) class
    >>declared without an intervening access-specifier are allocated so that
    >>later members have higher addresses within a class object."

    >
    >
    > Thank you. Then &b-&a makes sense for b declared after a. And sizeof(a)
    > would equal (&b-&a) if no padding were added?


    I don't see why not. Keep in mind that both 'a' and 'b' have to be in the
    same access "section". If they are in different sections or if they have
    the same access but there is another section between them, the &b-&a rule
    doesn't apply any more.

    V
    Victor Bazarov, Jan 21, 2005
    #7
  8. I haven't come across any compilers that reorder the member
    variables of a structure.

    However, most compilers do have options to choose between
    efficiency and memory requirement. For example, if accessing
    a 32 integer is efficient at 4 byte boundaries, the default
    option would be to align it that way. The compiler may let to align
    it at (say) a 1 byte boundary.

    This is applicable only on platforms that will allow misaligned
    transfers.

    Deepa
    --
    http://www.EventHelix.com/EventStudio
    EventStudio 2.5 - Automate sequence diagram generation
    EventHelix.com, Jan 22, 2005
    #8
    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. Boban Dragojlovic

    Dynamic Buttons and Event Handlers, revisited

    Boban Dragojlovic, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,858
    Boban Dragojlovic
    Jun 24, 2003
  2. Brian W
    Replies:
    1
    Views:
    2,903
    Yan-Hong Huang[MSFT]
    Jul 4, 2003
  3. B Wagner
    Replies:
    8
    Views:
    321
    B Wagner
    Aug 18, 2003
  4. puzzlecracker

    alignment revisited

    puzzlecracker, Jan 19, 2005, in forum: C++
    Replies:
    3
    Views:
    337
    puzzlecracker
    Jan 19, 2005
  5. puzzlecracker

    alignment revisited

    puzzlecracker, Jan 19, 2005, in forum: C Programming
    Replies:
    16
    Views:
    544
    Mark McIntyre
    Jan 20, 2005
Loading...

Share This Page