Talking about C++ Union

1. Consortium

The description of the union in the sixth edition of "C++ Primer Plus" is: a union is a data format that can store different data types, but only one of them can be stored at the same time.
The declaration format of union (ie, union) is as follows:

union MyUnion
{
    
    
	int iA;
	double dA;
	char cA;
};

The keyword union indicates that MyUnion is a union, MyUnion is a user-defined data type, and its usage is as follows:

MyUnion Ua;
Ua.iA=10;

According to the description of the sixth edition of "C++ Primer Plus", one of the uses of the consortium is to save space when data items use two or more formats (but not at the same time).
So what does this sentence really mean? Please see the following code:

union MyUnion
{
    
    
	int iA;
	char cA;
};
#include <iostream>

using namespace std;

int main()
{
    
    
	MyUnion UTestUnion;
	UTestUnion.iA=42;
	cout<<UTestUnion.cA<<endl;

	return 0;
}

This code declares a union named MyUnion. The main() function assigns the value of 42 to iA in the MyUnion variable UTestUnion. At this time, the iA of UTestUnion is equal to 42, then what is the cA of UTestUnion? The following is the output of the program:
Insert picture description here
Why is the output "*"?

The reason is that the elements in the union share the same memory.

The above code is slightly modified as follows:

union MyUnion
{
    
    
	int iA;
	double dA;
	char cA;
};

#include <iostream>

using namespace std;

int main()
{
    
    
	MyUnion UTestUnion;
	UTestUnion.dA = 42.35;

	double* dTest = (double*)&(UTestUnion.cA);
	cout << *dTest << endl;

	return 0;
}

In the above program, dTest is a pointer of type double, which points to the address of UTestUnion.cA. The address has undergone a coercive type conversion, and the original char type is converted to the double type. When dereferencing, the value stored in the address is obtained.
The output of the program is 42.35, which is the value of the double element dA assigned to UTestUnion.
This output is possible because the elements in the union share the same memory. The dA of UTestUnion is assigned a value of 42.35 and stored in the memory. The element cA of the char type also represents the value in this memory. It's just that because the char type and the double type are stored differently in the computer, although both cA and dA represent the same value in the memory, the storage method is different, so directly taking its value will behave differently.
The following figure is the result of debugging the program breakpoint in VS2019:
Insert picture description here
From the debugging result in the figure, we can see that the value of iA is unknown, and the value of cA is in the "?" format (unknown format) encoding -51. But after the address of cA is type-casted into a double type address, the value of dTest becomes the normal 42.35. This is precisely because dA and cA share the same memory. After the address of cA is changed to double, its active element becomes dA, so the following statement:

(double*)&(UTestUnion.cA);

Actually equal to

&(UTestUnion.dA)

summary

The consortium is very interesting, and there are definitely many functions that have not been discovered by the author. In case someone has a deeper insight, welcome to leave a message for guidance!

Guess you like

Origin blog.csdn.net/GeomasterYi/article/details/106796100