Union unions
The Union can be used when multiple data requires shared memory or when multiple data is taken only one at a time. This is described in the C programming Language book for the Consortium:
1) A consortium is a structure;
2) All its members have an offset of 0 relative to the base address;
3) This structure space is large enough to accommodate the most "wide" members;
4) Its alignment shall be suitable for all of its members;
These four descriptions are explained below:
Because all members in a union share a memory, the offset of each member's first address relative to the union variable is 0, that is, the first address of all members is the same. In order for all members to share a memory, the space must be sufficient to accommodate the widest members of these members. For this "alignment to fit all of the members" means that it must conform to all members ' own alignment.
The following examples illustrate:
such as the Consortium
char S[9 int n; double D;};
S is 9 bytes, n is 4 bytes, D is 8 bytes, so it requires at least 9 bytes of space. However, its actual size is not 9, and the operator sizeof tests its size to 16. This is because there is a problem with byte alignment, and 9 cannot be divisible by 4 or divisible by 8. So the bytes are added to 16 so that they align with the self of all members. It can be seen from here that the space occupied by a consortium depends not only on the widest member, but also on all members, i.e. its size must meet two conditions: 1) large enough to accommodate the widest member; 2) the size can be divisible by the size of all the basic data types it contains.
Test procedure:
/*Test Consortium 2011.10.3*/
#include <iostream>
using namespaceStd
Union U1
{
Chars[9];
intN
DoubleD
};
Union U2
{
Chars[5];
intN
DoubleD
};
intMainintargcChar*argv[])
{
U1 U1;
U2 U2;
printf"%d\n",sizeof(U1));
printf"%d\n",sizeof(U2));
printf"0x%x\n", &U1);
printf"0x%x\n", &U1.S);
printf"0x%x\n", &U1.N);
printf"0x%x\n", &u1.d);
u1.n=1;
printf"%d\n", u1.s[0]);
printf"%lf\n", u1.d);
UnsignedChar*p= (unsignedChar*) &u1;
printf"%d\n", *p);
printf"%d\n", * (p+1));
printf"%d\n", * (p+2));
printf"%d\n", * (p+3));
printf"%d\n", * (p+4));
printf"%d\n", * (p+5));
printf"%d\n", * (p+6));
printf"%d\n", * (p+7));
return 0;
}
The output is:
16
8
0x22ff60
0x22ff60
0x22ff60
0x22ff60
1
0.000000
1
0
0
0
48
204
64
0
Please press any key to continue ...
For sizeof (u1) = 16. Since S in U1 is 9 bytes, n is 4 bytes, D is 8 bytes, so at least 9 bytes are required. It contains a basic data type of char,int,double, respectively, 1,4,8 bytes, in order to make the size of the U1 occupied space can be 1,4,8 divisible, it is necessary to fill the bytes to 16, so sizeof (U1) =16.
For sizeof (U2) = 8. Since S in U2 is 5 bytes, n is 4 bytes, D is 8 bytes, so at least 8 bytes are required. It contains the basic data type of char,int,double 1,4,8 bytes respectively, in order to make the size of U2 occupied space can be 1,4,8 divisible, do not need to fill bytes, because 8 itself can meet the requirements. So sizeof (U2) = 8.
As can be seen from the base address of each of the printed members, each member of the consortium has the same base site, which is equal to the head of the union variable.
For U1.n=1, when the n assignment of U1 is 1, the first 4 bytes of that memory are stored in 00000001 00000000 00000000 00000000
So take s[0] data represents the first cell of the data, its integer value is 1, so the printed result is 1.
As for the print out of D for 0.000000, be willing to follow. Since the data for the first 4 bytes of memory is known to be 00000001 00000000 00000000 00000000, the result 48,204,64,0 can be found in the next 4 bytes of the data is 00110000 11001100 01000000 00000000, so it represents a binary floating-point number of
00000000 01000000 11001100 00110000 00000000 00000000 00000000 00000001
For double type data, 63rd bit 0 is the sign bit, 62-52 00000000100 is the order, 0000 11001100 00110000 00000000 00000000 00000000 00000001 is the mantissa, and the mantissa value is approximately 0 according to its value. , and the order code is 4-1023=-1019, so it represents a floating-point number of 1.0*2^ (-1019) =0.00000000000 ..., so the output is 0.000000.
Haizi Source: http://www.cnblogs.com/dolphin0520/
On the union in C language