what

Sep 27, 2018 at 9:34am
closed account (9G3v5Di1)
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?
Sep 27, 2018 at 9:42am
https://en.cppreference.com/w/cpp/language/union

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace 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
Sep 27, 2018 at 10:22am
closed account (9G3v5Di1)
thank you so much!
Sep 27, 2018 at 12:39pm
how is it that a and b have the same address?
Sep 27, 2018 at 12:48pm
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.

Sep 27, 2018 at 12:52pm
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.
Last edited on Sep 27, 2018 at 12:54pm
Sep 27, 2018 at 1:13pm
Hmm. I have just found another use for a union - @goldwinbryanr's other post at
http://www.cplusplus.com/forum/beginner/243198/#msg1078551

I wonder what he is learning this week!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdint>
using namespace std;

union Demo
{
   int8_t num[6] = { 72, 69, 76, 76, 79, 0  };
   char text[6];
};


int main()
{
   Demo test;
   cout << test.text;
}
Sep 27, 2018 at 1:36pm
A poor man's polymorphism.

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).

This use is entirely different:
1
2
3
4
5
union Demo
{
	__m64 part_sum;
	short part[4];
};

as in https://software.intel.com/en-us/articles/how-to-use-intrinsics

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.
Sep 27, 2018 at 3:50pm
wow never knew that :o thanks guys
Sep 27, 2018 at 4:13pm
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 ).




Last edited on Sep 27, 2018 at 4:22pm
Sep 28, 2018 at 6:07am
closed account (9G3v5Di1)
@lastchance I wonder how my topic has a title of "Pointers and references" but the given exercises were out of coverage
Last edited on Sep 28, 2018 at 6:11am
Sep 28, 2018 at 7:14am
What topic? The title of this thread is "what".

topic has a title of "Pointers and references"

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.

A hint: this task is not about pointers.
Sep 28, 2018 at 9:02am
Topic archived. No new replies allowed.