Archived

This topic is now archived and is closed to further replies.

Clarification on Unions

This topic is 5783 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

I just read about unions in one of my C++ books. They give a brief explanation of what they do yet don''t give any examples of their uses in an application. I understand that they are several variables that share the same space in memory, but how would that work? A simple explanation on a beginner''s level would help a lot. Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There are two main uses that I''m aware of:

(1) You want something that can hold a value of more than one type (but just one at a time).

For example, there is a struct in Win32 called a VARIANT that contains a type field that is just some numeric identifier for a type followed by a union containing a long, float, void*, etc. It looks like there are actually about 40 different types that it can contain. If they declared each one individually, that would probably be something like 40 types * about 4 bytes per type = about 160 bytes per instance. With a union, it''s probably more like 8 bytes for the value.

These are typically used in places where this kind of flexible is needed such as variables in a scripting language (java-script for example) or keeping a list of properties (without wanting to know about the specific type when storing, loading ,etc.).

(2) You want to be able to access the same value in different ways.

For example, there is a struct in Windows Sockets called an in_addr that represents an IP address. These are 32-bit values that are typically displayed in a more human-friendly format as four groups of 8-bit values (127.0.0.1).

I''ve personally used unions for both reasons. Once I wanted to load in a list of properties from a saved file so I used a union as described in use 1. Another time, I needed to expand a member of struct from one to an array of four but I couldn''t do so in a way that broke existing code. My solution was to create a union that contained the original declaration along with the array -- the original name now referred to the 0th element of the array. Best of both worlds!

Share this post


Link to post
Share on other sites
another example is writting a string class...

      
union UstringData
{
int i;
char c[sizeof(int)];
};


this is useful for copying the stringdata, since its 4 times faster to copy as int than as a char...

unions are basically around so that we dotn gotta do lots of ugly casting... which we tend to like to do, when people arent screaming "unions, bastard!!" at us.
  
int i;
char c3 = ((char*)&i)[3]; // sticks the 3rd byte of i into c3


//OR

union u { int i; char c[4]; } _u;
char c3 = _u.c[3];



Edited by - evilcrap on February 8, 2002 1:47:28 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You stated "I understand that they are several variables that share the same space in memory, but how would that work?"

Think of it this way. A garage.

You tell the designer that you want a garage that will hold either a sports car or a SUV.

The designer makes it is just large enough to hold the SUV, but the sports car will fit.

Note that you can only store 1 car there at at time.

The union command allocates enough memory on the heap for the largest size variable. HOW you use that memory is up to you, but you cannot magically store lots of stuff there, only 1.

Share this post


Link to post
Share on other sites