Union
A union can be used when multiple data needs to be shared with memory or multiple data are retrieved for only one time at a time ). In C Programming Language, the Union is described as follows:
1) A consortium is a structure;
2) The offset of all its members to the base address is 0;
3) The structure space should be large enough to accommodate the "widest" member;
4) The alignment method should be suitable for all the members;
The four descriptions are described below:
Because all members in the consortium share a piece of memory, the offset of the first address of each member to the base address of the consortium variable is 0, that is, the first address of all members is the same. To allow all members to share a piece of memory, the space must be sufficient to accommodate the widest member of these members. If the alignment method is suitable for all members, the alignment must be consistent with the alignment of all members.
The following is an example:
Such as Consortium
View sourceprint? Union U
{
Char s [9];
Int n;
Double d;
};
S occupies 9 bytes, n occupies 4 bytes, and d occupies 8 bytes. Therefore, it requires at least 9 bytes of space. However, the actual size is not 9, and the size is 16 by the sizeof operator. This is because of the byte alignment problem. 9 cannot be divisible by 4 or 8. Therefore, adding byte to 16 is in line with the alignment of all members. From this we can see that the space occupied by a consortium depends not only on the widest member, but also on all Members, that is, the size of the consortium must meet two conditions: 1) the size is sufficient to accommodate the widest member; 2) the size can be exceeded by the size of all basic data types it contains.
Test procedure:
/* Test the consortium 2011.10.3 */# include <iostream> using namespace std; union U1 {char s [9]; int n; double d ;}; union U2 {char s [5]; int n; double d ;}; int main (int argc, char * 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); unsigned char * p = (unsigned char *) & 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 ;}
Output result:
16
8
0x22ff60
0x22ff60
0x22ff60
0x22ff60
1
0.000000
1
0
0
0
48
204
64
0
Press any key to continue...
For sizeof (u1) = 16. In u1, s occupies 9 bytes, n occupies 4 bytes, and d occupies 8 bytes. Therefore, at least 9 bytes are required. The basic data types include char, int, and double, which occupy 1, 4, and 8 bytes respectively. In order to make the space occupied by u1 divisible by 1, 4, 8, you need to fill the byte to 16, so sizeof (u1) = 16.
For sizeof (u2) = 8. In u2, s occupies 5 bytes, n occupies 4 bytes, and d occupies 8 bytes. Therefore, at least 8 bytes are required. The basic data types include char, int, and double, which occupy 1, 4, and 8 bytes respectively. In order to make the space occupied by u2 divisible by 1, 4, and 8, bytes are not needed, because 8 can meet the requirements. Therefore, sizeof (u2) = 8.
The printed base address of each member shows that the base address of each member in the consortium is the same, which is equal to the first address of the consortium variable.
After the value of n of u1 is 1 for u1.n = 1, the data stored in the first four bytes of the memory in this segment is 00000001 00000000 00000000 00000000.
Therefore, the data of s [0] indicates the data of the first unit. The integer value is 1, so the output result is 1.
For the printed d is 0.000000, the following is acceptable. Since we know that the data stored in the first 4 bytes of memory in this segment is 00000001 00000000 00000000 00000000, We can print the result 48,204, 00110000 11001100 01000000 from the above, therefore, the binary floating point number is
00000000 01000000 11001100 00110000 00000000 00000000 00000000
For double-type data, 63rd-Bit 0 indicates the signed bit, 62-52 00000000100 indicates the order code, 0000 11001100 00110000 00000000 00000000 00000000 indicates the ending number, and the ending value is approximately 0 according to the value, the order code is 4-1023 =-1019, so the floating point number is 1.0*2 ^ (-1019) = 0. 00000000000 ......, the output result is 0.000000.
From: Haizi blog