Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

union doubt

This topic is 3639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, given a union consisting of a float , int , char .. how can we determine at a particular time which one of these (flaot, int , char) contains the valid value ?? thanks in advance ..

Share this post


Link to post
Share on other sites
Advertisement
All of them. This is what unions are for: you have a memory area which is both a float, an int and a char at the same time.

If, on the other hand, you want something which is either a float, or an int, or a char, you should probably look into boost::variant or implement your own variant type.

Share this post


Link to post
Share on other sites
Quote:
Original post by y2jsave
Hi All,
given a union consisting of a float , int , char .. how can we determine at a particular time which one of these (flaot, int , char) contains the valid value
??

thanks in advance ..
A room has 3 windows. Which window has a room on the other side of it?

Share this post


Link to post
Share on other sites
Technically, according to the standard (9.5 paragraph 1 for those wanting chapter and verse), a union only has one active member at a time. However, you have to keep track of which one is active yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Technically, according to the standard (9.5 paragraph 1 for those wanting chapter and verse), a union only has one active member at a time. However, you have to keep track of which one is active yourself.
So does that mean that using a union of int and float to do "fast" floating point square roots and other bit twiddling things is technically non-standard?

Share this post


Link to post
Share on other sites
Yup. Of course, you're in non-standard behavior land as soon as you start depending on floating point bit representations even without using unions, so adding a union on top of that doesn't really make things worse.

Share this post


Link to post
Share on other sites
One way to do it is through the use of a "discriminated union".


This entails the merging of a union and an enum into a struct. The value of the enum is changed in accordance with the active variable in the union. Then, one must simply check the value of the enum to see which variable is active.


enum foo {FLOAT,INT};

Union bar {
int i;
double d;
};

struct foobar{

foo activeVariable;
bar fdUnion;
}

foobar.fdUnion.d = 1.1;
foobar.activeVariable = DOUBLE;






Share this post


Link to post
Share on other sites
Quote:
So does that mean that using a union of int and float to do "fast" floating point square roots and other bit twiddling things is technically non-standard?
Well, bit-twiddling is non-standard by definition... but other than that, this is the ONLY standard compliant way of doing any such thing.

Also, with compilers that heavily optimize for strict aliasing (such as recent gcc versions), it is the only way to prevent yourself from not only shooting your foot, but shooting your foot in a way so the bullet will rip off your leg and your head at the same time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!