Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-26-2006, 11:17 AM
ravs2k6 ravs2k6 is offline
Newbie
 
Join Date: May 2006
Posts: 25
Rep Power: 9
ravs2k6 is on a distinguished road
Thumbs up What Is A Union

WHAT IS A UNION

A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.

You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.

A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need. Here is an example:

...
union time
{
long simpleDate;
double perciseDate;
}mytime;
....


The union above could be used to either store the current time (in seconds) to hold time accurate to a second. Or it could be used to hold time accurate to a millisecond. Presumably there are times when you would want one or the other, but not both. This declaration should look familiar. It is the same as a struct definition, but with the keyword union instead of struct.

ACCESSING UNION FIELDS

To access the fields of a union, use the dot operator(.) just as you would for a structure. When a value is assigned to one member, the other member(s) get whipped out since they share the same memory. Using the example above, the precise time can be accessed like this:

. . .
printTime( mytime.perciseDate );
. . .

In larger programs it may be difficult to keep track of which field is the currently used field. This is usually handled by using another variable to keep track of that. For example, you might use an integer called mode. When mode equals one, the regular date (simpleDate) is used. If mode is two, then perciseDate is used. This mode variable needs to be set every time a different member in the union is used. Here is a sample program to illustrate the use of unions.

#include <stdio.h>

int main()
{
union data
{
char a;
int x;
float f;
} myData;

int mode = 1;

myData.a = 'A';
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

myData.x = 42;
mode = 2;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

myData.f = 101.357;
mode = 3;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

if( mode == 1 )
printf("The char is being used\n");
else if( mode == 2 )
printf("The int is being used\n");
else if( mode == 3 )
printf("The float is being used\n");

return 0;
}
This little program declares a union with an int, float, and char. It uses each field, and after each use prints out all the fields (with one ugly printf statement). Here is some sample output:

Here is the Data:
A
577
0.000
Here is the Data:
*
42
0.000
Here is the Data:

1120581321
101.357
The float is being used

Your output might be different. Clearly the data in the unused fields is just garbage. This happens because different types are treated differently by the computer. So if one type is set, the memory is not going to be in the format of the other types. Also mode is used to keep track of the type. In this program it is pretty useless, but in larger programs it would be a great help in keeping track of the unions use.


Summary

Union allows same storage to be referenced in different ways

Only one way is valid at any given time

Usage
access individual bytes of larger type
variable format input records (coded records)
sharing an area to save storage usage
unions not used nearly as much as structures

Example

union HAH
{
int igor;
char chuck;
float felix;
};
...
union HAH Jody;
...


Jody.igor = 2175; /* felix and chuck not valid */
Jody.chuck = 'X'; /* igor and felix not valid */
Jody.felix = 2.5; /* igor and chuck not valid */

t = Jody.igor;

sizeof union is size of its biggest member
Unions most often contain different types of structures
Can only initialize first member of union
Can assign (copy) one union variable to another
Can pass union or pointer to union as function arg
Function can return union type
Can define pointers to union type object
Members accessed as unionvar.member or unionptr-$gt;member
Syntax, format and use of tags and declarators like struct, but members overlay each other, rather than following each other in memory

union HAH
{
short svar;
long lvar;
char cvar;
long double dvar;
};



Thus we see Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.

struct conditions
{
float temp;
union feels_like
{
float wind_chill;
float heat_index;
}
} today;

As you know, wind_chill is only calculated when it is "cold" and heat_index when it is "hot". There is no need for both. So when you specify the temp in today, feels_like only has one value, either a float for wind_chill or a float for heat_index.

Types inside of unions are unrestricted, you can even use structs within unions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-03-2006, 06:56 PM
Kaabi's Avatar   
Kaabi Kaabi is offline
Programming God
 
Join Date: Jul 2006
Posts: 884
Rep Power: 13
Kaabi is on a distinguished road
Default

I'd heard of structures, but not of unions. I haven't encountered them yet in my studies of C++ (or are they only in C, as you are describing the C language?)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-08-2006, 01:14 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,035
Last Blog:
NaNoWriMo Day 23 The...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Unions exist in C++. By wrapping a union in a class you can get a "smart union" that will know what mode its storage is in at the moment and automatically work appropriately.
Code:
class Meta {
  private:
    union data {
      int i;
      char c;
      float f;
      char* p;
    };
    enum Mode {INT, CHAR, FLOAT, PTR};
    Mode m;
}
Now by overloading =, <<, and >> a few times, you can have a meta class that stores its data in a union and keeps track of what type of data it's storing automatically.

Assume your operators have been overloaded (mainly cause I'm feeling lazy):
Code:
int main()
{
  Meta supervar;
  float myfloat=1.3;
  int myint=73;
  char mychar='a';
  char* ptr=&mychar;
  supervar=myfloat;  //supervar.data.f=1.3, supervar.m=FLOAT
  std::cout<<supervar; //outputs 1.3
  supervar=mychar; //supervar.data.c='a', supervar.m=CHAR
  std::cout<<supervar; //outputs 'a'
  return 0;
}
This is the trick you CAN'T do in C, but can do in C++. Whether you've saved any memory after all that is another matter, but if used a lot is better than having a half ton of switchs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
For those in the USA: Last night's Presidential 'State of the Union' techni68 The Lounge 0 01-24-2007 03:49 AM


All times are GMT -5. The time now is 12:06 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 66%

Ads