Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Union Misalignment?

Reply
Thread Tools

Union Misalignment?

 
 
Bryan Parkoff
Guest
Posts: n/a
 
      09-15-2005
byte and word and double word are always in alignment. Do C++ Compiler
fill the zero as padding inside byte's field and word's field to avoid
misalignment? It looks like that union does not use shift which it loads
data from memory. It is like "(H << | L".
Intel Pentium 4 can degrade the performance if shift is used heavily,
but union may be recommended to avoid using shift. It may be slow to store
two bytes into memory before load one word from memory containing two bytes.
union example is below.
Please advise.

union var
{
struct
{
unsigned __int8 L;
unsigned __int8 H;
};
struct
{
unsigned __int16 W;
};
};

Bryan Parkoff


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      09-16-2005
On Thu, 15 Sep 2005 16:29:57 GMT, "Bryan Parkoff" <(E-Mail Removed)>
wrote in comp.lang.c++:

> byte and word and double word are always in alignment. Do C++ Compiler
> fill the zero as padding inside byte's field and word's field to avoid
> misalignment? It looks like that union does not use shift which it loads
> data from memory. It is like "(H << | L".


That is not something specified by the language. It depends on the
compiler, and quite possibly on options that you use when you run the
compiler.

> Intel Pentium 4 can degrade the performance if shift is used heavily,
> but union may be recommended to avoid using shift. It may be slow to store
> two bytes into memory before load one word from memory containing two bytes.
> union example is below.
> Please advise.


The C++ language knows nothing at all about the Intel Pentium 4 or any
other particular processor. How well a particular compiler optimizes
the object code for any particular processor is a QOI (Quality Of
Optimization) issue. The language standard only specifies what the
observable output of a strictly conforming program must be. It does
not specify how fast the output must be produced or how small the
executable image must be.

> union var
> {
> struct
> {
> unsigned __int8 L;
> unsigned __int8 H;
> };
> struct
> {
> unsigned __int16 W;
> };
> };
>
> Bryan Parkoff


I have heard anecdotally that Intel's compilers, for example, generate
more optimized code for Intel processors than Microsoft's compilers
do. I have no knowledge whether this is correct.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
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
SQL UNION query in Java goli Java 4 04-05-2005 09:54 AM
SQL UNION -like filtering of offline DataSet into DataView for WebControl binding Marco Ippolito ASP .Net 2 05-19-2004 12:35 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
THE STATE OF THE UNION Jenna Bush MCSE 95 01-30-2004 02:02 PM



Advertisments