C union float trick?

Started by
20 comments, last by sit 18 years, 5 months ago
Thanks Cocalus & folks. I tried it as a C++ project and it returns an undefined value. So looks like it's a C only trick. I suppose they did it for speed.

cplusplus.com sayz, "All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration."

Darrell
Advertisement
Quote:Original post by ZQJ
This is in fact illegal C++ (not sure about C), however most compilers support it because it's a common trick. The reason is that compilers can optimize by assuming variables of different types cannot alias to the same memory location, but in this case it's fairly easy for the compiler to handle.

Edit: beaten.


Skimming over the C99 spec, so far it appears legal. The rules for structures apply to unions, so if a struct can have a float and an int, so can a union.

Quote:
6.7.2.1 Structure and union specifiers

7. A member of a structure or union may have any object type other than a variably modified type.93) In addition, a member may be declared to consist of a specified number of bits (including a sign bit, if any). Such a member is called a bit-field;94) its width is preceded by a colon.

93) A structure or union can not contain a member with a variably modified type because member names are not ordinary identifiers as defined in 6.2.3.

13. The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members or if a member is a bitfield, then to the unit in which it resides), and vice versa.


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by Roboguy
You would use pointers.

Yeah, sorry if I was vague about that. The C++ trick us the same as the C trick, just uses reinterpret explicitly.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
LessBread - could u please provide a link to the C99 Standard? I am not sure I am holding the correct thing, even after googling for it. Thanks.
It isn't a freely avaiable document. The C++ Standard can be purchased from the ISO for something small like $15.
For a draft version of the C99 spec google for n869.pdf, for C++ spec ansi_cpp.pdf or poke around at Open Standards. The quote comes from n869.pdf.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Also from n869.pdf "6.5.2.3 Structure and union members"
5 With one exception, if the value of a member of a union object is used when the most recent store to the object was to a different member, the behavior is implementation-defined 70). One special guarantee is made in order to simplify the use of unions: If a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

70) The "byte orders" for scalar types are invisible to isolated programs that do not indulge in type punning (for example, by assigning to one member of a union and inspecting the storage by accessing another member that is an appropriately sized array of character type), but have to be accounted for when conforming to externally imposed storage layouts.

Implementation-defined means different compilers can do different things with this code. You have to check the documentation of your compiler to find out what it will do.
___________________________Buggrit, millennium hand and shrimp!
static_cast<float>(asdasdasd);
Quote:Original post by Raistmaj
static_cast<float>(asdasdasd);

That preserves the value (precision allowing), not the underlying bit-pattern.

Enigma
seeing this, its no wonder that Q3 uses such a lot of bandwidth

void MSG_WriteFloat( msg_t *sb, float f ) {	union {		float	f;		int	l;	} dat;		dat.f = f;	MSG_WriteBits( sb, dat.l, 32 );}


This makes the point of delta compression, which they are said to use, completely useless
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement