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,054
  • 4
  • 51
  • 63
Nick Heiner
  • 114,397
  • 181
  • 465
  • 693

5 Answers5

8

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
  • 302,239
  • 39
  • 506
  • 752
  • 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 Jun 18 '10 at 17:34
  • 2
    "trap representation" – 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
  • 127,537
  • 50
  • 225
  • 279
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
  • 38,339
  • 11
  • 92
  • 126
  • In this case, the struct doesn't have a name. How would I access it? – Nick Heiner Jun 18 '10 at 17:26
  • 1
    @Rosarch: Technically it's illegal in C. It's legal in C++, in which case the names are exposed as members of `class Odp`. – Billy ONeal Jun 18 '10 at 17:29
  • 1
    @Billy ONeal: Incorrect. "Anonymous structs" are illegal in C++. C++ supports anonymous unions, but not anonymous structs. The OP's declaration is ill-formed. – AnT Jun 18 '10 at 17:31
  • @John Weldon: The link in your answer doesn't suggest anything, until you get to "jsmith's" post, which correctly states that the code is illegal. – AnT Jun 18 '10 at 17:36
  • I concede, given that I don't know the answer definitively :) – John Weldon Jun 18 '10 at 17:38
  • 1
    @John Weldon: In your answer, `Odp o1();` declares a function, not an object. Somehow It think this wasn't your intent. – AnT Jun 18 '10 at 17:39
  • @AndreyT; do you want to review any of my other answers? You're on a streak :D – John Weldon Jun 18 '10 at 17:41
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,510
  • 19
  • 38