9

Whenever I see examples of union, they are always different types. For example, from MSDN:

// declaring_a_union.cpp
union DATATYPE    // Declare union type
{
    char   ch;
    int    i;
    long   l;
    float  f;
    double d;
} var1;          // Optional declaration of union variable

int main()
{
}

What happens if I have a union (in this case anonymous, but that shouldn't matter) like this:

union
{
    float m_1stVar;
    float m_1stVarAlternateName;
};

Regardless of whether this is good practice or not, will this cause any issues?

timrau
  • 22,578
  • 4
  • 51
  • 64
Samaursa
  • 16,527
  • 21
  • 89
  • 160
  • What issues do you think this might cause? – James McNellis Jun 28 '11 at 20:32
  • I am getting heap corruption, and the answer to this question will lead to another question :) ... I want to eliminate all possibilities. In this case, I don't see any issues, but as they say 'you never know'. – Samaursa Jun 28 '11 at 20:34
  • I guess, you are already using some tools apart from mere code inspection? On unix-likes valgrind is incredibly useful, not sure about alternatives on Windows (http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows). Finding heap corruption by code inspection can be very time-consuming. – Tilman Vogel Jun 28 '11 at 20:46

3 Answers3

12

No, this won't cause any issues. The reason you don't see it more often is that it's pointless - both names refer to the same value of the same type.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 10
    True, but sometimes you may not know whether the types are the same (like if one of them is a template argument). – leftaroundabout Jun 28 '11 at 20:37
  • 2
    @leftaroundabout, very good point that I hadn't considered. Thanks. – Mark Ransom Jun 28 '11 at 20:39
  • 2
    Sorry to necro this, but can this be backed up? Is it defined somewhere? Obviously it should work, but is it _guaranteed_? – Shaggi Jan 07 '16 at 22:34
  • 1
    @Shaggi since it follows the required syntax, the only way it wouldn't work is if it were specifically disallowed. I've never been much of a language lawyer, so I'm not really keen to search the standard for something that probably isn't there. – Mark Ransom Jan 07 '16 at 22:52
  • 1
    I should have specified: Will there be a problem if I write to the first field, and read from the second? I was going to ask this question, but I'm pretty sure it would be closed, being a duplicate of this. – Shaggi Jan 07 '16 at 23:04
  • 1
    @Shaggi I've heard that writing to one element of a union and reading from another is actually undefined behavior, although you might get away with it. – Mark Ransom Jan 08 '16 at 03:19
  • @Shaggi can you link your posted question – fat_flying_pigs Feb 25 '17 at 07:40
  • 1
    @fat_flying_pigs http://stackoverflow.com/questions/34677343/accessing-same-type-inactive-member-in-unions – Shaggi Feb 25 '17 at 13:50
  • @Shaggi Your question is different. This one doesn't have structs. – HolyBlackCat Apr 28 '17 at 16:36
2

This is legal and very useful in situations where you have different contexts, where different names would be more appropriate. Take a four member vector vec4 type for example (similar to what you'd find in GLSL):

vec4 v(1., 1., 1., 1.);

// 1. Access as spatial coordinates
v.x;
v.y;
v.z;
v.w;

// 2. Access as color coordinates (red, green, blue, alpha)
v.r;
v.g;
v.b; 
v.a;

// 3 Access as texture coordinates
v.s;
v.t;
v.p;
v.q;

A vec4 may only have four members of the same type, but you'd use different names to refer to the same objects, depending on the context.

Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
0

An issue would arise only if you want to have unique values for the two variables. In your use-case, it should work fine.