Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ALIGNMENT REVISITED 2

Reply
Thread Tools

ALIGNMENT REVISITED 2

 
 
puzzlecracker
Guest
Posts: n/a
 
      01-19-2005

> 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-19-2005
puzzlecracker wrote:
> [...]
> what is POD?


Try to learn to use groups.google.com to look up answers that
have already been given multiple times.
 
Reply With Quote
 
 
 
 
EventHelix.com
Guest
Posts: n/a
 
      01-21-2005
The following article should help:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation

 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      01-21-2005
"EventHelix.com" <> wrote in message
news: oups.com...
> The following article should help:
>
> http://www.eventhelix.com/RealtimeMa...ndOrdering.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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2005
Gary Labowitz wrote:
> "EventHelix.com" <> wrote in message
> news: oups.com...
>
>>The following article should help:
>>
>>http://www.eventhelix.com/RealtimeMa...ndOrdering.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
 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      01-21-2005
"Victor Bazarov" <> wrote in message
news:Rp9Id.37366$ o.verio.net...
> Gary Labowitz wrote:
> > "EventHelix.com" <> wrote in message
> > news: oups.com...
> >
> >>The following article should help:
> >>
> >>http://www.eventhelix.com/RealtimeMa...ndOrdering.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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2005
Gary Labowitz wrote:
> "Victor Bazarov" <> wrote in message
> news:Rp9Id.37366$ o.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
 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      01-22-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NVIDIA Forceware Revisited article Silverstrand Front Page News 0 09-22-2005 01:37 PM
alignment revisited puzzlecracker C Programming 16 01-20-2005 10:40 PM
alignment revisited puzzlecracker C++ 3 01-19-2005 03:16 PM
Mnenhy revisited Caploc Firefox 3 12-16-2004 12:37 AM
REVISITED: connecting a dell powerconnect 3024 to a cisco 2924M over fiber Brad Tarver Cisco 0 07-09-2003 12:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57