Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Clarification on Unions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 pizza box   Members   -  Reputation: 168

Like
Likes
Like

Posted 05 February 2002 - 02:07 PM

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

Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 05 February 2002 - 04:37 PM

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!


#3 pizza box   Members   -  Reputation: 168

Like
Likes
Like

Posted 06 February 2002 - 01:31 PM

Thanks for the help

#4 EvilCrap   Members   -  Reputation: 134

Like
Likes
Like

Posted 07 February 2002 - 06:45 PM

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

#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 12 February 2002 - 05:46 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS