6

If I have a class:

class Odp
{
    int i;
    int b;
    union
    {
         long f;
         struct
         {
               WCHAR* pwszFoo;
               HRESULT hr;
         };
    };

}

Union means that, of all values listed, it can only take on one of those values at a time? How does that work in terms of accessing these variables? How would I access hr directly? If I set hr, what happens if I try to access f?

timrau
  • 22,578
  • 4
  • 51
  • 64
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

6 Answers6

9

This is a very fraught area in the C++ standard - basically a union instance, per the standard can only be treated at any one time as if it contained one "active" member - the last one written to it. So:

union U {
   int a;
   char c;
};

then:

U u;
u.a = 1;
int n = u.a;
u.c = 2;
char c = u.c;

is OK, but:

U u;
u.a = 1;
char c = u.c;

is not. However, there are vast volumes of existing code that say that both are OK. and in neither, or any, case will an exception be thrown for an "invalid" access. The C++ language uses exceptions exceptionally (!) sparingly.

Basically, if you find yourself using unions in your C++ code to deal with anything but C libraries, something is wrong.

4

Every time you set (write to) a member of a union, you essentially make it "active". You are only allowed to read the currently active member of the union. It means that it is your responsibility to remember somehow which member is active at each moment in time.

Attempting to access the inactive member of a union leads to undefined behavior.

Keep in mind also that your code is not valid C++. There's no such thing as "anonymous struct" in C++. Your struct member has to have a name. If your compiler accepts it, it is just a non-standard extension supported by your specific compiler.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Hmm... I knew the result would be to return undefined data, but AFAIK the act of accessing the other member shouldn't cause undefined behavior like accessing the target of a null pointer does. – Billy ONeal Jun 18 '10 at 17:31
  • 3
    @Billy ONeal: Yes, it should, for obvious reasons. Since the types are generally unrelated, the other (inactive) member can easily end up with trap representation. Which is the main (and obvious) reason the behavior is undefined. – AnT stands with Russia Jun 18 '10 at 17:34
  • 2
    "trap representation" <-- Never heard of that one before. +1 – Billy ONeal Jun 18 '10 at 21:09
2

Right, with a union the same memory locations will be used to represent a single one of the members at any given time. So if you have an instance of the union and set the value of hr, you will get garbage if you then try to read the value of f.

Try using the following to access hr:

union a;
a.hr = NULL;
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
1

It just means you can access the same memory as either the long, or the struct.

To access hr:

Odp o1;
o1.hr;

Interesting link: http://www.cplusplus.com/forum/general/18816/

John Weldon
  • 39,849
  • 11
  • 94
  • 127
0

Attempt to access "f" will give you some result. It will likely be representation other member of union as data type of "f", i.e. in this case you are likely will be reading partial or entire content of the "pwszFoo" represented as "long" data type. General concept is easy - union members share the same location in memory.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
0

The Union will allocate enough memory for the largest type (thing) in the union. So you may have many types of objects that have large memory foot prints and you will only be passing one of them at a time to some other place in your code. The Union lets you do that. It's one step up from passing a void pointer. In either case you need to devise a way to known what is stored in an instance of a Union. The code below is a simple way to do that by wrapping the Union in a structure. The structure defines an enumeration to identify the item used by the Union and provides a place to store the enumerated type.

    union UnionItem {
      int a;
      float b;
      double c;
   };

   struct UnionObj {
      enum Type{
         I, F, D
      };
      Type t;
      UnionItem item;
   };

   UnionObj o;
   o.item.b = 2.3f;
   o.t = UnionObj::F;

   // Usually the UnionObj would be passed to other functions or methods.

   switch (o.t) {
   case UnionObj::I:
      cout << o.item.a;
      break;
   case UnionObj::F:
      cout << o.item.b;
      break;
   case UnionObj::D:
      cout << o.item.c;
      break;
   default:
      cout << "Something wrong!";
   }
Scott
  • 789
  • 6
  • 7