ALIGNMENT REVISITED 2

P

puzzlecracker


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
 
G

Gary Labowitz

EventHelix.com said:

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.
 
V

Victor Bazarov

Gary said:
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
 
G

Gary Labowitz

Victor Bazarov said:
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?
 
V

Victor Bazarov

Gary said:
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
 
E

EventHelix.com

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top