Union is mainly shared memory, allocating memory to its largest structure or object size, that is, the largest sizeof. In the process of writing A/C + + program, when multiple basic data types or composite data structures occupy the same piece of memory, we use a consortium, and when multiple types, multiple objects, and many things are taken for one (let's call it "N-Select 1"), we can also use a consortium to play its advantage. Reprint from the website: When multiple basic data types or composite data structures occupy the same piece of memory, we use a consortium when several types, multiple objects, and multiple things take one (we call them "N-Select 1").
You can use consortia to play their strengths. First look at a piece of code:
Union Myun
{
struct {int x; int y; int z;} U
int k;
}a;
int main ()
{
a.u.x = 4;
A.U.Y = 5;
A.u.z = 6;
A.K = 0;
printf ("%d%d%d\n", a.u.x,a.u.y,a.u.z);
return 0;
}
The Union type is shared memory, with the largest structure of size as its own size, in this case, Myun this structure contains the structure of U, and the size is equal to the size of u this structure, in the memory of the Order of the Declaration of x,y,z from low to high, and then assign value, in memory, That's where X's position placed the 4,y position to place the 5,z position 6, now the K assignment, the assignment of K because it is union, to share the memory, so starting from the first address of the Union, the first address is actually the position of x, so the original memory x position is replaced by the value of K, To become 0, this time to print, directly to see the memory line, x position is the position of K is 0, and y,z position of the value has not changed, so it should be 0,5,6
Look at two more questions:
Test one: Write a program to determine whether the CPU in the system is little endian or big endian mode.
Analysis:
As a computer-related professional, we should have learned in the computer composition of what is called little endian and big endian. Little endian and big endian are two different sequences of data that the CPU holds. For data types such as Integer and Long Integer, the big endian that the first byte is the highest byte (the high byte to the low byte in the order of the data from the low address to the high address), and little endian, It considers the first byte to be the lowest byte (the low byte to the high byte of data stored in the order from the low address to the high address).
For example, suppose the following data starts with the memory address 0x0000:
0x12 0x34 0xAB 0xCD
If we read a four byte variable with an address of 0x0000, if the byte order is Big-endian, the result is 0X1234ABCD, and if the byte order bit is Little-endian, the result is 0xcdab3412. If we write 0X1234ABCD to memory starting with 0x0000, the results of the little endian and the big endian mode are as follows:
Address 0x0000 0x0001 0x0002 0x0003
Big-endian 0x12 0x34 0xAB 0xCD
Little-endian 0xCD 0xAB 0x34 0x12
In general, the x86 series CPUs are Little-endian byte sequence, PowerPC is usually big endian, and some CPU can set the CPU through the jumper to work in the little or the big endian mode.
Answer:
Obviously, the only way to solve this problem is to store a byte (char/byte type) of data and an integer data in the same memory
Start the address, by reading integer data, analysis Char/byte data in the high or low level of integer data to judge CPU work in Little
Endian or big endian mode. Come to the following answer:
typedef unsigned char BYTE;
int main (int argc, char* argv[])
{
unsigned int num,*p;
p = #
num = 0;
* (BYTE *) p = 0xFF;
if (num = 0xff)
{
printf ("The endian of CPU is little\n");
}
else//num = = 0xff000000
{
printf ("The endian of CPU is big\n");
}
return 0;
}
In addition to the above method (by casting the pointer type and assigning a value to the first byte of the integer data, whether the assignment is assigned to a high or low level), is there any
There's a better way. We know that the members of the union themselves are stored in the same memory space (shared memory, where union functions and contribute), so that we can combine a char/byte data and an integer data as members of a union, drawing
The following answer:
int Checkcpu ()
{
{
Union W
{
int A;
Char b;
C
C.A = 1;
return (C.B = 1);
}
}
To achieve the same functionality, let's look at how the source code in the Linux operating system is done:
Static Union {char c[4]; unsigned long mylong;} endian_test = {{' L ', '? ', '? ', ' B '}};
#define ENDIANNESS ((char) endian_test.mylong)
Linux Kernel authors use only a union variable and a simple macro definition to achieve a large piece of code the same function. From the above section of code we can profoundly understand the Linux source code of subtlety. (If endianness= ' l ' indicates that the system is little endian,
For ' B ' means big endian)
Question two: Assume that network Node A and network Node B communication protocol involving four categories of messages, message format is "Message Type field + message content structure", four message content structure type is structtype1~ STRUCTTYPE4, please write the program in the simplest way group
Weave a unified message data structure.
Analysis:
The format of the message is "message type + structure of message content", in real communication, only one of the four types of messages can be sent each time, we could organize the structure of the four class message into a union (share a memory, but each valid is only one), and then the message Type field is organized into a packet data structure.
Answer:
Based on the above analysis, we naturally come to the following answers:
typedef unsigned char BYTE;
Message Content Consortium
typedef Union Tagpacketcontent
{
STRUCTTYPE1 PKT1;
STRUCTTYPE2 pkt2;
STRUCTTYPE3 PKT1;
STRUCTTYPE4 pkt2;
}packetcontent;
Unified message Data structure
typedef struct TAGPACKET
{
BYTE PktType;
Packetcontent pktcontent;
}packet;