473,327 Members | 2,007 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,327 software developers and data experts.

type conversion within a union

All,

I have the following structure definition:

struct S
{
union U
{
double fval;
int ival;
} u;
};

Let's say I declare one like so:

struct S s;
s.u.fval = 5.0;

And later do this:

s.u.ival = (int)s.u.fval;

And the converse:

s.u.ival = 23;
s.u.fval = (double)s.u.ival;

I am unsure if it is legal to perform this conversion over the top of union
fields that share the same storage space? Can anyone clarify?

It is simple enough to change my code to not require a union, this is more
of a curiosity right now..

thanks,
James

Aug 16 '07 #1
1 4165
"James Brown" <em***@viahomepage.comwrites:
I have the following structure definition:

struct S
{
union U
{
double fval;
int ival;
} u;
};

Let's say I declare one like so:

struct S s;
s.u.fval = 5.0;

And later do this:

s.u.ival = (int)s.u.fval;

And the converse:

s.u.ival = 23;
s.u.fval = (double)s.u.ival;

I am unsure if it is legal to perform this conversion over the top of
union fields that share the same storage space? Can anyone clarify?
[...]

It's perfectly valid. You're reading the value of one union member,
and then storing the (converted) value in another union member; the
two operations won't interfere with each other.

Things that invoke undefined behavior are (C99 6.5p2):

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

but you're not doing that here.

However, I'd drop the casts, and let the conversions be done
implicitly. It's tempting to add a cast to emphasize that a
conversion is happening; the problem is that the compiler doesn't
check that you're using the correct type. If you change ival from int
to long, but forget to change the cast so you're still converting to
int before storing the value, your code could break. If you leave the
cast out, the implicit conversion will always be the right one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 16 '07 #2

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

Similar topics

5
by: JasBascom | last post by:
if i have an ifstream object ifstream validdata; const int list = 5; Allrecords books; Allrecords *rec = books; if i had the following code validdata.seekg(0, ios::end);
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
5
by: Preben | last post by:
Hi, I get this error when trying to compile: -------- # g++ -c KGreyImage.cpp KGreyImage.cpp:25: error: expected constructor, destructor, or type conversion before '*' token --------
1
by: zaeminkr | last post by:
I have a question about type conversion function for user defined type. I have two classes class DRect { private : double x0; double y0;
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
2
by: algatt | last post by:
Hello, I am trying to compile the TPIE files but there is a file that's constantly giving errors about the templates. I am using gcc 3.4.5 on Eclipse using Windows XP. The following is the code of...
11
by: 1230987za | last post by:
Hi, I am totally confused now about C type conversion. I know that C does some implicit type conversion like integer promotion and float to double. I imagine that such conversion must keep the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.