Sorry - I'm sure this is very basic to someone who codes in C++. I'm auditing an ETX self paced course for intro to C++. I ran across this in a question about 'what's the value of myUnion.intValue at line x (indicated by comment below). And I answered 3 initially and it was wrong. I've stepped through this and once you assign the value to myUnion.doubleValue the value for myUnion.intValue sets to something odd (depending on what you stuffed into myUnion.doubleValue). It acts like I'm trying to use the wrong type variable for the number. But "double" is just a float, and 4.5 (and other values I've tried) are perfectly reasonable float numbers. What the heck is the super-simple thing I'm missing here.
Yes, I did search on google, etc but came up dry using my methods at least. Here's the code -
#include <iostream>
usingnamespace std;
int main()
{
union numericUnion
{
int intValue;
long longValue;
double doubleValue;
};
numericUnion myUnion;
myUnion.intValue = 3;
// intValue is now 3
cout << myUnion.intValue << endl;
myUnion.doubleValue = 4.5;
// this is the line x I'm referring to above.
// intValue is now something other than 3
cout << myUnion.doubleValue << endl;
cout << myUnion.intValue << endl;
}
This is not a homework assignment, it's a question on a section quiz. I am not getting a grade or a cert or anything, just trying to audit and learn. So... not cheating.
A union is a user-defined type in which all members share the same memory location. This means that at any given time a union can contain no more than one object from its list of members. It also means that no matter how many members a union has, it always uses only enough memory to store the largest member.
Basically, as soon as you write the double, it replaces the int.
Any value is a representation of its bit combination. In your case int and long might be bit wise compatible, but int and double are certainly not compatible. So you cannot write double where an int is expected and vice versa.
unions allocate one block of memory large enough to hold the largest item in the union. Everything inside shares this memory, so it can only BE one of the types at any ONE time. In very strict c++, a union variable can only be one of the types ever for its lifetime. All compilers I know of support reusing it for the other types; and its most useful feature to me has always been the ability to use it to get to the raw bytes of the other type, eg to serialize simple types with minimal effort.
In your case int and long might be bit wise compatible
Even if the int is 32 bits and the long is 64, if they're stored in little-endian byte order the int will basically contain the value of the long modulus 2**32.
Thank you - I knew as soon as someone saw this they'd know exactly what was up. I had glossed over the union bit and was focusing on the data types. Thanks to the heads-up, I went over the union section again and it made perfect sense. In normal programming I'm not yet seeing a real need to use unions (it says they help in low memory situations) but I'll probably discover more about it as I go on.
Thanks!
-Jeff
you should not need a union in anything you write. You just need to know enough to handle one if you have old code or C code that used one -- you probably have all you need to know already.