473,396 Members | 1,748 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,396 software developers and data experts.

Real Life Unions


A recent post asking for help with unions reminded me of this
component of the C language that I have only used a couple of times,
and those almost entirely out of personal whim -- Unions for the sake
of Unions simply because I wanted to see one in action.

Granted: it makes it possible to save a few bytes of storage when you
have something that can be a chicken or a rooster, but not both, and
you're always going to know which it is.

But I don't think I've ever encountered a problem where I smacked
myself in the head and said, "A union would be perfect for this!" It
always seems like a design error to go jamming different types into a
single symbol, or at least a design that's asking for trouble.

That said, I don't write device drivers, kernels, or software for
embedded devices. Space is not usually the constraint that concerns
me: clarity, maintainability, and buglessness are almost always my #1
goals.

But I was curious, in the community of c programmers, whether Unions
had an honored place in the engineer's toolkit, or whether they were
more like an old awl kicking around the dark corners of the box. Feel
free to provide instructive examples of great moments in Union history!

Mar 9 '07
67 3283
Richard Heathfield wrote:
Yevgen Muntyan said:
>Keith Thompson wrote:
>>Adrian Hawryluk <ad**************************@nospam.comwrites:
Richard Heathfield wrote:
Adrian Hawryluk said:
<snip>
>
>Thanks for the info.
>>
>However, I and it looks like a few others don't see it your way
>that unions are not topical.
Huh?!?!? Of course unions are topical! See, for example,
6.7.2.1. There's even a 'union' keyword. Unions not topical? Never
heard such tosh.
>
My apologies, that was miss directed. It was Kenny that said:
[snip]

Kenny is an admitted troll; he is best ignored.
Note that in this case he caught even Richard Heathfield

It isn't a matter of "catching". It seemed to me that Mr McCormack's
article was attempting to make a legitimate, albeit mistaken, point
about C.
You do understand the very point of Kenny's statement: you
can do without unions anything you can do with them. He used word
"struct", indeed, so you got a chance to correct him. But then
how about my "counterexample"? If we're as pedantic as you love
to be (when you find it convenient), then your counterexample
was wrong.
If you really really are saying that you wanted to correct
the factual error in Kenny's statement, then you're either
lying or you do as you usually do - posts where every letter
is correct but post as whole doesn't carry any useful
information. If you wanted to fight a troll a little, then
just say so. I doubt anyone cares about you being always
always correct, I'd think people respect you for other things,
certainly not for your terrible nitpicking attitude.

Best regards,
Yevgen
Mar 13 '07 #51
Richard Heathfield wrote:
Old Wolf said:
.... snip ...
>>
You regularly make posts in response to others who omit the
furniture, pointing out that they are causing UB by calling
printf without a prototype in scope, etc.

Yes, that's right, I do - when there is good reason to suspect
that they are unaware of the importance of the furniture. When
it is evident to me that the poster knows the language, I don't
do this. I do not see this as being inconsistent.
Hmm, you've done it to me. What conclusions should I draw?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '07 #52
Richard Heathfield wrote:
Kenny McCormack said:
>Adrian Hawryluk <ad**************************@nospam.comwrote:
...
>>However, I and it looks like a few others don't see it your way
that unions are not topical.

Whoever said that?

What *I* said, which you may have misunderstood, was that there's no
*use* of unions that is topical here that can't just as easily be done
with a struct.

Counter-example:

union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');
But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored. The proper way to
perform that is:

double d;
char *dcp;
size_t i;

dcp = &d;
for (i = 0; i < sizeof(double); i++) printf("%02X", *(dcp + i));
putchar('\n');

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '07 #53
CBFalconer said:
Richard Heathfield wrote:
>Old Wolf said:
... snip ...
>>>
You regularly make posts in response to others who omit the
furniture, pointing out that they are causing UB by calling
printf without a prototype in scope, etc.

Yes, that's right, I do - when there is good reason to suspect
that they are unaware of the importance of the furniture. When
it is evident to me that the poster knows the language, I don't
do this. I do not see this as being inconsistent.

Hmm, you've done it to me. What conclusions should I draw?
Several benign possibilities (into which it would be tedious to go) do
exist, so there is no need to panic at this stage. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #54
CBFalconer said:
Richard Heathfield wrote:
>>
Counter-example:

union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored.
No, it isn't illegal. See 3.3.2.3. "With one exception, if a member of a
union object is accessed after a value has been stored in a different
member of the object, the behavior is implementation-defined." Since
when did we translate "implementation-defined" as "illegal"? After all,
printf("%d\n", INT_MAX); is implementation-defined...

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #55
Richard Heathfield wrote:
union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');
I think you should present this with the "furniture",
/as an example of best practice/ on CLC.

--
Chris "electric hedgehog" Dollin
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Mar 14 '07 #56
Chris Dollin said:
Richard Heathfield wrote:
>union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

I think you should present this with the "furniture",
/as an example of best practice/ on CLC.
And since you asked so nicely, Chris, I'm very glad to oblige:

#include <stdio.h>

union U { double d; unsigned char ir[sizeof(double)]; };

int main(void)
{
union U u;
size_t i;
u.d = 3.14159;
puts("The obj rep of (double)3.14159 on this system is:");

for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

return 0;
}
On my system, the above program produced the following output:

The obj rep of (double)3.14159 on this system is:
6E861BF0F9210940

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #57
Richard Heathfield wrote:
>
CBFalconer said:
>Richard Heathfield wrote:
>>>
Counter-example:

union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored.

No, it isn't illegal. See 3.3.2.3. "With one exception, if a
member of a union object is accessed after a value has been
stored in a different member of the object, the behavior is
implementation-defined." Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...
It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.

BTW, you might consider making your standard references correspond
to some version younger than 18. For the benefit of others, try
6.5.2.3, constraint #5.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '07 #58
Yevgen Muntyan wrote:
[...]
You do understand the very point of Kenny's statement: you
can do without unions anything you can do with them. He used word
"struct", indeed, so you got a chance to correct him. But then
how about my "counterexample"? If we're as pedantic as you love
to be (when you find it convenient), then your counterexample
was wrong.
[...]

What about importing some data file that you get from a customer
and/or vendor? The file layout is such that every "record" is the
same length, the first X chars are an indicator of how the rest of
the "record" are to be handled, and the actual layout of the rest
of the "record" varies based on the type indicator.

Yes, to be truly 100% portable without the need to give any special
compiler flags, or worry about padding or byte orders or anything
else, I guess you really need to fread() the "record" into a buffer,
and then manually memmove() things to where you want.

However, assuming that you can coerce the compiler into laying out
a struct that exactly matches the "record" layout (which is not an
unreasonable assumption even in "the real world"), the use of a
single struct, rather than a union of different structs for each
layout, is needless overhead.

Yes, you _can_ do it that way. It just makes more sense to use
the union. (Why move things around to other structs, when the
data is already there in the union?)

You could also do away with for loops, "?:" expressions, all data
types other than "int" and "float" (simply make them the largest
integer and floating types available), and lots of other things
in the language.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 14 '07 #59
Kenneth Brody wrote:
Yevgen Muntyan wrote:
[...]
>You do understand the very point of Kenny's statement: you
can do without unions anything you can do with them. He used word
"struct", indeed, so you got a chance to correct him. But then
how about my "counterexample"? If we're as pedantic as you love
to be (when you find it convenient), then your counterexample
was wrong.
[...]

What about importing some data file that you get from a customer
and/or vendor? The file layout is such that every "record" is the
same length, the first X chars are an indicator of how the rest of
the "record" are to be handled, and the actual layout of the rest
of the "record" varies based on the type indicator.

Yes, to be truly 100% portable without the need to give any special
compiler flags, or worry about padding or byte orders or anything
else, I guess you really need to fread() the "record" into a buffer,
and then manually memmove() things to where you want.

However, assuming that you can coerce the compiler into laying out
a struct that exactly matches the "record" layout (which is not an
unreasonable assumption even in "the real world"), the use of a
single struct, rather than a union of different structs for each
layout, is needless overhead.
Yes.
Yes, you _can_ do it that way.
Yes.
It just makes more sense to use
the union. (Why move things around to other structs, when the
data is already there in the union?)
Yes.
You could also do away with for loops, "?:" expressions, all data
types other than "int" and "float" (simply make them the largest
integer and floating types available), and lots of other things
in the language.
Yes.

That's the funny thing, "you can do without unions anything you can do
with them", which is true. It's also true that it'd be stupid not
to use what is more appropriate. Look at this:

Kenny wrote:
The point is that there is nothing topical in this newsgroup that you
can do with a union that you couldn't do with a struct (at the cost of
using more memory/space - another thing we're not allowed to talk
about here).
While it's clear that "with a struct" was a plain mistake (not even a
mistake in strictest sense, when we get as pedantic as possible), "not
allowed" part is not completely true, "regulars" are allowed to talk
about anything they want here; he was correct in the very idea that
you can avoid using unions, and any justification for using union
is either off-topic (like memory saving) or a matter of style (like
X events example). It's also true that what your example is
completely off-topic here, even more off-topic than usual, since you
are saving/loading structure content and that's non-portable, breaks
across implementations, may break even on the same implementation, etc.

Kenny made a good point about it, perhaps not in the best form, but
that's Kenny, "admitted troll it's best to ignore him" as some say. The
OP was very lucky that "regulars" were in good mood, or maybe common
sense worked in this case for some reason. Or perhaps they interpreted
it as "what are strictly-conforming examples of using union", which
is unlikely to be what OP asked about.

Yevgen
Mar 14 '07 #60
CBFalconer <cb********@yahoo.comwrites:
Richard Heathfield wrote:
>>
CBFalconer said:
>>Richard Heathfield wrote:

Counter-example:

union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored.

No, it isn't illegal. See 3.3.2.3. "With one exception, if a
member of a union object is accessed after a value has been
stored in a different member of the object, the behavior is
implementation-defined." Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...

It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.
Um, what is what to you? Are you saying that

printf("%d\n", INT_MAX);

is "illegal"? There might be a number of reasons to avoid writing
code whose behavior depends on the value of INT_MAX, but it's hardly
illegal.

[...]

--
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"
Mar 14 '07 #61
Richard Heathfield <rj*@see.sig.invalidwrites:
Chris Dollin said:
[...]
>I think you should present this with the "furniture",
/as an example of best practice/ on CLC.

And since you asked so nicely, Chris, I'm very glad to oblige:

#include <stdio.h>

union U { double d; unsigned char ir[sizeof(double)]; };

int main(void)
{
union U u;
size_t i;
u.d = 3.14159;
puts("The obj rep of (double)3.14159 on this system is:");

for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

return 0;
}
On my system, the above program produced the following output:

The obj rep of (double)3.14159 on this system is:
6E861BF0F9210940
Mine too (though on another system I got 400921F9F01B866E).

To be painfully pedantic, I'll point out that your program implicitly
assumes CHAR_BIT==8 (it prints two hex digits per byte). In most
real-world applications that's a perfectly reasonable assumption, but
here's a version that should work properly even on more exotic
systems:

#include <stdio.h>
#include <limits.h>

union U { double d; unsigned char ir[sizeof(double)]; };

int main(void)
{
union U u;
size_t i;

const int nybbles_per_byte = CHAR_BIT / 4 + (CHAR_BIT % 4 != 0);
/* Yeah, there's probably a cleaner way to do that. */

u.d = 3.14159;
puts("The obj rep of (double)3.14159 on this system is:");

for (i = 0; i < sizeof(double); i++) {
if (i != 0) putchar(':');
printf("%0*X", nybbles_per_byte, u.ir[i]);
}
putchar('\n');

return 0;
}

Note that I've inserted :' characters between bytes in the output,
resulting in:

6E:86:1B:F0:F9:21:09:40

I'm not able to test it on a CHAR_BIT>8 system, but it does work as
expected if I manually change the value of nybbles_per_byte.

--
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"
Mar 14 '07 #62
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Richard Heathfield wrote:
>>CBFalconer said:
Richard Heathfield wrote:
Counter-example:
>
union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');
But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored.
No, it isn't illegal. See 3.3.2.3. "With one exception, if a
member of a union object is accessed after a value has been
stored in a different member of the object, the behavior is
implementation-defined." Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...
It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.

Um, what is what to you? Are you saying that

printf("%d\n", INT_MAX);

is "illegal"? There might be a number of reasons to avoid writing
code whose behavior depends on the value of INT_MAX, but it's hardly
illegal.
If implementation says that after "u.d = 3.14159;" u.ir will get you
a zero-filled array, then this code gets a serious problem. Of course,
"illegal" is the word you are arguing to, not what Chuck says about
evil implementation which breaks code...

Yevgen
Mar 14 '07 #63
Yevgen Muntyan <mu****************@tamu.eduwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>Richard Heathfield wrote:
[...]
>>> Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...
It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.
Um, what is what to you? Are you saying that
printf("%d\n", INT_MAX);
is "illegal"? There might be a number of reasons to avoid writing
code whose behavior depends on the value of INT_MAX, but it's hardly
illegal.

If implementation says that after "u.d = 3.14159;" u.ir will get you
a zero-filled array, then this code gets a serious problem. Of course,
"illegal" is the word you are arguing to, not what Chuck says about
evil implementation which breaks code...
It's not inconceivable that the representation of 3.14159 happens to
be all-bits-zero, but I fail to see what that has to do with my
question. I was *only* asking Chuck to clarify his remark about
printf("%d\n", INT_MAX);

This is, of course, a public forum, and I don't mean to imply that
only Chuck is allowed to answer a question that I addressed to him,
but you just didn't address what I said.

--
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"
Mar 14 '07 #64
Keith Thompson wrote:
Yevgen Muntyan <mu****************@tamu.eduwrites:
>Keith Thompson wrote:
>>CBFalconer <cb********@yahoo.comwrites:
Richard Heathfield wrote:
[...]
>>>> Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...
It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.
Um, what is what to you? Are you saying that
printf("%d\n", INT_MAX);
is "illegal"? There might be a number of reasons to avoid writing
code whose behavior depends on the value of INT_MAX, but it's hardly
illegal.
If implementation says that after "u.d = 3.14159;" u.ir will get you
a zero-filled array, then this code gets a serious problem. Of course,
"illegal" is the word you are arguing to, not what Chuck says about
evil implementation which breaks code...

It's not inconceivable that the representation of 3.14159 happens to
be all-bits-zero,
I didn't say that. I have C89 words quoted by Richard in mind:

With one exception, if a member of a union object is accessed after a
value has been stored in a different member of the object, the behavior
is implementation-defined.

It looks reasonable to assume that "get an object which consists
of the same bits" is not the only possible (DS9000?) behavior.
but I fail to see what that has to do with my
question. I was *only* asking Chuck to clarify his remark about
printf("%d\n", INT_MAX);
Well, you replied not to the post where he said that, but to
the post where he said he doesn't want "to be dependant on the
whims or profit motives of a William Gates", i.e. it seemed clear
he didn't want to insist on "illegal", but he still didn't like
"implementation-defined" part... I might have misunderstood
it.

Yevgen
Mar 14 '07 #65
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Richard Heathfield wrote:
>>CBFalconer said:
Richard Heathfield wrote:
>
Counter-example:
>
union U { double d; unsigned char ir[sizeof(double)]; };
union U u;
size_t i;
u.d = 3.14159;
for(i = 0; i < sizeof(double); i++)
{
printf("%02X", u.ir[i]);
}
putchar('\n');

But, strictly speaking, that's illegal, because you are reading
from a variant other than that last stored.

No, it isn't illegal. See 3.3.2.3. "With one exception, if a
member of a union object is accessed after a value has been
stored in a different member of the object, the behavior is
implementation-defined." Since when did we translate
"implementation-defined" as "illegal"?
After all, printf("%d\n", INT_MAX); is implementation-defined...

It is to me. I don't want the action of my software to be
dependant on the whims or profit motives of a William Gates.

Um, what is what to you? Are you saying that

printf("%d\n", INT_MAX);

is "illegal"? There might be a number of reasons to avoid writing
code whose behavior depends on the value of INT_MAX, but it's
hardly illegal.
No, I was referring to the 'counter example', which is
implementation defined in the extraction from the union. I thought
that was obvious.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 15 '07 #66
Yevgen Muntyan wrote:
[...]
X events example). It's also true that what your example is
completely off-topic here, even more off-topic than usual, since you
are saving/loading structure content and that's non-portable, breaks
across implementations, may break even on the same implementation, etc.
Just because the data file generated by a particular program is
not portable does not mean that the program itself isn't. (After
all, one could argue that a simple fprintf(out,"Hello, world.\n")
isn't "portable" in that the file it generates depends on the
particular character set and EOL sequence of the platform.) If
a program can write something to a file, and then later read the
same file and get the same information, it doesn't really matter
if that file can't be taken to a different platform and read by
the same program compiled on the other platform. (At least not
as far as standard C is concerned. The developer/end-user may
be concerned about data-file portability, but that a different
matter entirely.)

If you use fwrite() to save a float to a binary stream, and later
in the program use fread() on that same stream (assuming that the
file position is the same), aren't you guaranteed to get the same
float value back? (Assuming nothing else has modified the file,
and the fread succeeds, of course.)
Kenny made a good point about it, perhaps not in the best form, but
that's Kenny, "admitted troll it's best to ignore him" as some say. The
OP was very lucky that "regulars" were in good mood, or maybe common
sense worked in this case for some reason. Or perhaps they interpreted
it as "what are strictly-conforming examples of using union", which
is unlikely to be what OP asked about.
I don't think there's anything OT about writing unions to develop
a messaging interface, or other system-specific things like a GUI
interface. Naturally, the implementation of the messages, events,
and GUI itself are OT here, but I don't think an interface to such
things are OT.

For example:

Ignoring the actual GUI specifics, could I use a union of
structs to implement a generic "draw this shape" interface?

Yes, you could to that in 100% "standard C" and be on-topic for
clc.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 15 '07 #67
Kenneth Brody wrote:
Yevgen Muntyan wrote:
[...]
>X events example). It's also true that what your example is
completely off-topic here, even more off-topic than usual, since you
are saving/loading structure content and that's non-portable, breaks
across implementations, may break even on the same implementation, etc.

Just because the data file generated by a particular program is
not portable does not mean that the program itself isn't. (After
all, one could argue that a simple fprintf(out,"Hello, world.\n")
isn't "portable" in that the file it generates depends on the
particular character set and EOL sequence of the platform.) If
a program can write something to a file, and then later read the
same file and get the same information, it doesn't really matter
if that file can't be taken to a different platform and read by
the same program compiled on the other platform. (At least not
as far as standard C is concerned. The developer/end-user may
be concerned about data-file portability, but that a different
matter entirely.)
It may be non-portable even between program invocations. But it's
details. I am *not* trying to argue that you may not talk about
such things here, no way. The problem is that in this newsgroup
the following is totally possible:

Richard Heathfield asks: Why would you use union?
I say: to write and read binary data in my program which draws
pictures.
Richard Heathfield says: Off-topic here therefore irrelevant.
I say: X events example.
Richard Heathfield says: Off-topic here therefore irrelevant.

This one is totally true, there is one fresh thread demonstrating
it.
If you use fwrite() to save a float to a binary stream, and later
in the program use fread() on that same stream (assuming that the
file position is the same), aren't you guaranteed to get the same
float value back? (Assuming nothing else has modified the file,
and the fread succeeds, of course.)
You are, certainly. But if *reason* for doing this doesn't happen
to be likable, you're told you're wrong.
>Kenny made a good point about it, perhaps not in the best form, but
that's Kenny, "admitted troll it's best to ignore him" as some say. The
OP was very lucky that "regulars" were in good mood, or maybe common
sense worked in this case for some reason. Or perhaps they interpreted
it as "what are strictly-conforming examples of using union", which
is unlikely to be what OP asked about.

I don't think there's anything OT about writing unions to develop
a messaging interface, or other system-specific things like a GUI
interface. Naturally, the implementation of the messages, events,
and GUI itself are OT here, but I don't think an interface to such
things are OT.

For example:

Ignoring the actual GUI specifics, could I use a union of
structs to implement a generic "draw this shape" interface?
Note that it's not what was asked. Question was about real-life uses
of unions, which *is* off-topic (once some "regular" doesn't happen
to like/need those uses, of course).
Yes, you could to that in 100% "standard C" and be on-topic for
clc.
But you can't provide on-topic reasons for preferring unions over
structs, or preferring union to "normal" way to interpret an object
as character sequence (elsewhere I was told union trick for this
was meant to be UB, that's totally funny thing).

Anyway, please read my post you actually replied to. It wasn't
"it's off-topic here". If it did sound like that, I apologize.
I in no way want to tell what is and what isn't off-topic in
this newsgroup.

Yevgen
Mar 15 '07 #68

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

Similar topics

6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
3
by: Gérard Talbot | last post by:
Hello all, When webfonts are used for purely cosmetic/ornemental reasons and on a large scale, I don't agree. When webfonts are used because Unicode support among browsers for a particular...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.