Union??

I can't remember ...
What does the union { ... } do?

How is it used???
LVL 1
lar_jensAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
a union is a vay of having more than one data type reside in the same memory structure.  For instance, say you want to be able to store an array of "things".  Some of these are char, some are int, some are floats.  A union can permit this:

union unionMultiType {
  char ch;
  int i;
  float fl;
};


Now you can use it:

union unionMultiType myunion;

you can access the different parts of the union like a struct:

myunion.ch gets your the char part.  Since the union allocates memory based on the largest type that needs to fit in it, the size depends on what you put in it.  Remember, however, that unlike a struct, all of the possible types in a union take up the same "space" so there is only room for one of the types at a time.

0
 
lar_jensAuthor Commented:
Thanks...
I remember now..
0
 
nietodCommented:
>> more than one data type reside in the same
>> memory structure.
I think he means "same memory location"

Except in rare instances, like when dealing with low level stuff, like hardware or the OS, there is no place for unions in C++.  In general, avoid unions at all costs.  The have many potential problems, like they don't work well with classes that have cosntructors.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jhanceCommented:
nietod,

Maybe you should talk with Microsoft about their practices.

COM uses unions, it's not low level, nor is it hardware or OS specific....

typedef struct FARSTRUCT tagVARIANT VARIANT;
typedef struct FARSTRUCT tagVARIANT VARIANTARG;

typedef struct tagVARIANT  {
    VARTYPE vt;
    unsigned short wReserved1;
    unsigned short wReserved2;
    unsigned short wReserved3;
    union {
        Byte                        bVal;                        // VT_UI1.
        Short                        iVal;                        // VT_I2.
        long                        lVal;                        // VT_I4.
        float                        fltVal;                    // VT_R4.
        double                    dblVal;                    // VT_R8.
        VARIANT_BOOL            boolVal;                    // VT_BOOL.
        SCODE                        scode;                    // VT_ERROR.
        CY                            cyVal;                    // VT_CY.
        DATE                        date;                        // VT_DATE.
        BSTR                        bstrVal;                    // VT_BSTR.
        DECIMAL                    FAR* pdecVal            // VT_BYREF|VT_DECIMAL.
        IUnknown                    FAR* punkVal;            // VT_UNKNOWN.
        IDispatch                 FAR* pdispVal;            // VT_DISPATCH.
        SAFEARRAY                FAR* parray;            // VT_ARRAY|*.
        Byte                        FAR* pbVal;                // VT_BYREF|VT_UI1.
        short                        FAR* piVal;                // VT_BYREF|VT_I2.
        long                        FAR* plVal;                // VT_BYREF|VT_I4.
        float                        FAR* pfltVal;            // VT_BYREF|VT_R4.
        double                    FAR* pdblVal;            // VT_BYREF|VT_R8.
        VARIANT_BOOL            FAR* pboolVal;            // VT_BYREF|VT_BOOL.
        SCODE                        FAR* pscode;            // VT_BYREF|VT_ERROR.
        CY                            FAR* pcyVal;            // VT_BYREF|VT_CY.
        DATE                        FAR* pdate;                // VT_BYREF|VT_DATE.
        BSTR                        FAR* pbstrVal;            // VT_BYREF|VT_BSTR.
        IUnknown                 FAR* FAR* ppunkVal;    // VT_BYREF|VT_UNKNOWN.
        IDispatch                 FAR* FAR* ppdispVal;// VT_BYREF|VT_DISPATCH.
        SAFEARRAY                 FAR* FAR* pparray;    // VT_ARRAY|*.
        VARIANT                    FAR* pvarVal;            // VT_BYREF|VT_VARIANT.
        void                            FAR* byref;                    // Generic ByRef.
char                            cVal;                            // VT_I1.
unsigned short                uiVal;                        // VT_UI2.
unsigned long                ulVal;                        // VT_UI4.
int                            intVal;                        // VT_INT.
unsigned int                uintVal;                        // VT_UINT.
char FAR *                    pcVal;                        // VT_BYREF|VT_I1.
unsigned short FAR *    puiVal;                        // VT_BYREF|VT_UI2.
unsigned long FAR *        pulVal;                        // VT_BYREF|VT_UI4.
int FAR *                    pintVal;                        // VT_BYREF|VT_INT.
unsigned int FAR *        puintVal;                    //VT_BYREF|VT_UINT.
    };
};
 
0
 
lar_jensAuthor Commented:
I just wondered what the union inside
struct in_addr was for.. Now I know..
I gives you the opprtunity to manipulate
an ip-address as :

4 bytes (8bit)
2 words (16bit) or
one long word (32bit)..

I excuse the terms here, but they've
been there ever since I started
assembly programming on Amiga.. =)
0
 
jhanceCommented:
In this case, it's just 3 different ways of looking at the same data.

An IP address is a 32-bit number.  Sometimes it's convenient to look at it as a 32-bit value.  Other times, it's more convenient to use the 4 8-bit values, like with 192.168.0.1.  That's 4 byte-wide values displayed in DECIMAL numbers.

I've not had a use for the 2 16-bit words but I'm sure someone has.
0
 
lar_jensAuthor Commented:
Could it be MicroSoft??
0
 
nietodCommented:
>> Maybe you should talk with Microsoft
>> about their practices.
Thats why I said OS.  OSs have a tendancy to use union types to save spae.  unfortunately they tend to cause a lot of headaches for less experienced programmers.  Especially in an OOP languages.  
0
 
lar_jensAuthor Commented:
I'm sorry, but the comment was ment as
a joke, NOT to be taken seriously...

It was also a comment about using
and IP adress as two 16bit values,
not about using union..

Keep up the good work everyone..
0
All Courses

From novice to tech pro — start learning today.