Tuesday 27 September 2011

Union in C/C++

What Is a Union?


1. Like a structure, a union is also a derived data type.
2. The members of a union share a single storage space.
3. Only ONE member of each union can be referenced at a time.
4. Amount of space allocated for storage is the amount needed for the largest
member of the union.

C/C++

In C and C++, untagged unions are expressed nearly exactly like structures (structs), except that each data member begins at the same location in memory. The data members, as in structures, need not be primitive values, and in fact may be structures or even other unions. However, C++ does not allow for a data member to be any type that has a full fledged constructor/destructor and/or copy constructor, or a non-trivial copy assignment operator. For example, it is impossible to have the standard C++ string as a member of a union.
The primary usefulness of a union is to conserve space, since it provides a way of letting many different types be stored in the same space. Unions also provide crude polymorphism. However, there is no checking of types, so it is up to the programmer to be sure that the proper fields are accessed in different contexts. The relevant field of a union variable is typically determined by the state of other variables, possibly in an enclosing struct.


Structure and union specifiers have the same form. [ . . . ] 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 (computer science) at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.One common C programming idiom uses unions to perform what C++ calls a reinterpret_cast, by assigning to one field of a union and reading from another, as is done in code which depends on the raw representation of the values. A practical example is the method of computing square roots using the IEEE representation. This is not, however, a safe use of unions in general.



Example of the Use of a Union

union temperature
{
short int surfaceOfEarthTemperature;
long int astronomicalTemperature;
float floatingPointTemperature;
};
union temperature celsiusTemperature, fahrenheitTemperature, ovenTemperature,
surfaceOfTheSunTemperature;
main()
{
celsiusTemperature.floatingPointTemperature = 87.3;
fahrenheitTemperature.floatingPointTemperature =
32.0 + (9.0 * celsiusTemperature.floatingPointTemperature/5.0);
ovenTemperature.ssurfaceOfEarthTemperature = 375;
surfaceOfTheSunTemperature.astronomicalTemperature = 4387912;
}

// anonymous_unions.cpp
#include <iostream> 
using namespace std;
int main()
{ 
      union {
              int d; char *f;
            };
             d = 4; 
             cout << d << endl; 
             f = "inside of union";
             cout << f << endl; 
}


union <name>
{ 
     <datatype> <1st variable name>; 
     <datatype> <2nd variable name>;  
     . . . 
     <datatype> <nth variable name>;
}<union variable name>;


union name1 
{
    struct name2 
    { 
       int a; 
       float b;
       char c;      
    } 
     svar; 
     int d; 
} uvar;

No comments:

Post a Comment