473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Union Misalignment?

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 << 8) | 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
Sep 15 '05 #1
1 2124
On Thu, 15 Sep 2005 16:29:57 GMT, "Bryan Parkoff" <no****@nospam.com>
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 << 8) | 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
Sep 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
2
by: dave | last post by:
trying to make adjustments to my 'position fixed' nav menu, but can't seem to get it centered, no matter how hard I try: this jumbled-mess can be viewed at: http://hardwares.po.gs/menu_test/ ...
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.