Write a program that will display the address of a float variable and another variable that shares the same address and value as the first variable. Do not initialize the first variable.
how do i create another variable that is the same address as the first one?
#include <iostream>
usingnamespace std;
union Demo
{
double a;
double b;
};
int main()
{
Demo twoDoubles;
twoDoubles.b = 3.14159;
cout << "The value of the first double is " << twoDoubles.a << '\n';
cout << "The address of the first double is " << &twoDoubles.a << '\n';
cout << "The value of the second double is " << twoDoubles.b << '\n';
cout << "The address of the second double is " << &twoDoubles.b << '\n';
}
The value of the first double is 3.14159
The address of the first double is 0x28ff28
The value of the second double is 3.14159
The address of the second double is 0x28ff28
That is what a union does, @adam2016. All variables in the union use (or, at least, start at) the same bytes in memory. Changing any one of them will change those bytes of memory, and hence the values of the other variables that share them.
Please see the cppreference given.
There used to be a similar statement in Fortran, called "equivalence" - mainly used where memory is scarce, so you could re-use it. That statement has been well and truly deprecated.
I'm afraid that I've never found a use for a union in real code, but maybe someone can suggest some good uses.
I saw a messaging system once that used unions. If I remember right, it was something along these lines:
1 2 3 4 5 6 7 8 9 10
struct message
{
int messageType;
union messageBody
{
message_about_beans msgBeans;
message_about_toast msgToast;
message_about_eggs msgEggs;
}
}
One would read the messageType to know what kind of message one was dealing with, and act accordingly:
1 2 3 4 5 6 7 8 9 10 11
switch ( receivedMsg.messageType)
{
case 0:
// read and deal with a message_about_beans object
break;
case 1:
// read and deal with a message_about_toast object
break;
// etc
}
A poor man's polymorphism. The codebase was primarily Objective-C, I think.
That could be called variant type too. Bit like in Python, you can store (not even near almost) anything in the variable (but you have to remember what type it was, when you read it back).
That union offers two different ways to address same logical data. One (the Demo::part) is for us and the other (Demo::part_sum) is for intrinsic CPU instructions; for explicit vectorization. The C++ standard was rather open about alignment, but latest standard versions support explicit alignment, which (AFAIK) should reduce the need for unions.
you can do anything that a union can do another way. I always liked them, but given that the union hack that I grew up with has been deemed 'undefined behavior' (even though 100% of compilers support it correctly) their usefulness dried up with that decree.
the above union is identical (in practice) to
__m64 part_sum;
short *part = (short*)&part_sum;
part[0]; // this is the same as the union's part[0]
They are not generally useful but if you find yourself coding at the byte level (lets say you want to endian convert some integers across platforms on a RISC machine that can't do it for you), they have some merits... or if you want part of a large result (say you have a SHA type algorithm that spews out a number that is too big for practical use, maybe you want every other byte of the result to make it smaller to use as a hash key ).
Write a program that will display the address of a float variable and another variable that shares the same address and value as the first variable. Do not initialize the first variable.