473,386 Members | 1,609 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.

equality of fields and elements in union - uni_vect.y == uni_vect.v[1]

Hi,
I have found a piece of code which declares union like this:
union vector_s {
struct {float x,y,z};
float m[3];
} v;
there is unnamed structure m ( matrix 1x3 ) which has same offset as
x, so they are aligned properly. The problem I have is, whether I can
safely write data to, lets say, v.m[2] and read same data from v.z?
Like:
v.m[2] = 1.25f;
assert (v.z == 1.25f);
I tried to find answer myself, but i was not successfull.
Please, could you enlighten me?
thank you

Oct 9 '07 #1
2 1400
c.***@seznam.cz wrote, On 09/10/07 07:34:
Hi,
I have found a piece of code which declares union like this:
union vector_s {
struct {float x,y,z};
float m[3];
} v;
C does not have anonymous struct/union members so you actually need:
union vector_s {
struct {float x,y,z;} s;
float m[3];
} v;
there is unnamed structure m ( matrix 1x3 ) which has same offset as
x, so they are aligned properly. The problem I have is, whether I can
safely write data to, lets say, v.m[2] and read same data from v.z?
Like:
v.m[2] = 1.25f;
assert (v.z == 1.25f);
I tried to find answer myself, but i was not successfull.
Please, could you enlighten me?
You cannot do it safely since the compiler is allowed to insert padding
between structure members but not between array members. The standard
does not even require that it has a good reason to do it! So whilst
v.m[0] will be at the same address as v.s.x, there is no guarantee that
v.m[1] is at the same address as v.s.y

The correct thing to do is either use an array OR a struct. If you want
to use a struct but still have names you can do
enum {X, Y, Z, NO_ELEMENTS};
float v[NO_ELEMENTS];
Then use v[Z]

Finally, it is almost always better to use double. A lot of the time
floats get promoted to double and where they are different szes float is
often *slower* than double.
--
Flash Gordon
Oct 9 '07 #2
c.***@seznam.cz wrote:
I have found a piece of code which declares union like this:
union vector_s {
struct {float x,y,z};
float m[3];
} v;
there is unnamed structure m ( matrix 1x3 ) which has same offset as
x, so they are aligned properly. The problem I have is, whether I can
safely write data to, lets say, v.m[2] and read same data from v.z?
Like:
v.m[2] = 1.25f;
assert (v.z == 1.25f);
I tried to find answer myself, but i was not successfull.
On top of what Keith Thompson and Flash Gordon said, the reason that
your assert fails *might* be that in your implementation, v.z simply
does *not* equal 1.25f (unlikely, since 1.25 is easily representable in
binary, but I take your example as an illustration and 1.25 may in
actual code be a different constant). You can verify it by something like:

float squiggle = 1.25f;
assert(squiggle == 1.25f);

Try:

v.m[2] = 1.25f;
assert(v.z == v.m[2]);
Oct 14 '07 #3

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

Similar topics

9
by: Gleep | last post by:
sorry i didn't explain it correctly before my table is like this example fields: ID name username outcome date1 date2 date3 (etc..) - date15 price1 price2 price3 (etc..) I know that...
5
by: windandwaves | last post by:
Hi Gurus I am new to mysql. I need to select the lowest number from seven fields where the value is greater than 0. Can someone tell me how to do this. So far, I have Select RF1, RU1,...
8
by: Bill | last post by:
Hello out there; This may be a challenge but I'm certain it's possible but I can't seem to figure out how. I have a table that has several date fields, e.g., Date1, Date2, Date3, Date4 ......
0
by: jimmyshaw83 | last post by:
I am sorry if this topic is tired, but after all that I have read I am still in the woods here. I have built a database to handle weekly and monthly reports of what people are doing. They submit...
2
by: Stu Smith | last post by:
A feature we'd like to see in VS 2004 is a compiler warning for equality comparisons on floating values. Why? Because the behaviour allowed under the ECMA spec is somewhat suprising. (Well it...
4
by: Matt Burland | last post by:
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled with different object (i.e. ComboBox1 contains...
6
by: onetitfemme | last post by:
Hi *, I have been looking for a definition or at least some workable concept of "XML equality". Searching on "XML equality" in comp.text.xml, microsoft.public.xsl and microsoft.public.xml...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.