Question 1:
Write a program to determine whether the CPU in the system is in the little endian or big endian mode?
Analysis:As a computer-related professional, we should have learned nothing in computer composition.
Endian and big endian. Little endian and big
Endian is the order in which the CPU stores data. Big
Endian considers that the first byte is the highest byte (from the low address to the high address order to store the data in the high byte to the low byte); While little
On the contrary, endian considers the first byte to be a byte (from low to high ). For example, assume that the following data exists from the memory address 0x0000:
| Zero x 0000 |
Zero X 0001 |
Zero X 0002 |
Zero X 0003 |
| 0x12 |
0x34 |
0xab |
0xcd |
If we read a four-Byte variable whose address is 0x0000, if the byte order is big-
Endian, the read result is 0x1234abcd; if the byte order is little-Endian, the read result is 0xcdab3412. If we
0x1234abcd is written to the memory starting with 0x0000. The storage results of the little endian and big endian modes are as follows:
| Address |
Zero x 0000 |
Zero X 0001 |
Zero X 0002 |
Zero X 0003 |
| Big-Endian |
0x12 |
0x34 |
0xab |
0xcd |
| Little-Endian |
0xcd |
0xab |
0x34 |
0x12 |
Generally, x86 series CPUs are in the byte order of little-Endian, PowerPC is usually big endian, and some CPUs can use jumpers to set whether the CPU works in the little endian or big endian mode.
Answer:Obviously, the answer to this question can only be one byte (Char/byte type) of data and an integer
Type data is stored in the same memory start address. By reading integer data, we can analyze whether the char/byte data is at the high or low level of integer data to determine whether the CPU is working at Little
Endian or big endian mode. Get the following answer: typedef unsigned char byte; int main (INT argc, char * argv []) {unsigned int num, * P; P = & num; 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 (the pointer type is forcibly converted and the first byte value is assigned to the integer data, it is determined that the value is assigned to a high level
Is it low? We know that union members are themselves stored in the same memory space (shared memory is the place where Union plays a role and contributes), because
Here, we can use a char/byte data and an integer data as a union member at the same time to get the following answer: int checkcpu (){
{
Union W
{
Int;
Char B;
} C;
C. A = 1;
Return (C. B = 1 );
} To implement the same function, let's see how the source code in the Linux operating system is implemented: static Union {char C [4]; unsigned long l ;} endian_test = {'l ','? ','? ',' B '}}; # define endianness (char) endian_test.l) linux Kernel authors only use a union variable and a simple macro definition to implement the same function of a large segment of code! From the above code, we can deeply understand the subtlety of Linux source code!
Question 2:
Assume that the communication protocols in network node A and Network Node B involve four types of packets. The message format is "Message Type field + message content structure", and the structure types of the four packets are structtype1 ~ Structtype4: compile a program to organize a unified packet data structure in the simplest way.
Analysis:The message format is "Message Type + message content structure". In real communication, only one of the four types of packets can be sent at a time, we can organize the structures of the four types of packets into a union (sharing a piece of memory, but each time only one type is valid), and then form a Data Structure in a unified manner with the packet type fields.
Answer:Based on the above analysis, we naturally come to the following answer: typedef unsigned char byte ;//
Message content consortium typedef Union tagpacketcontent {structtype1 pkt1; structtype2 pkt2; structtype3 pkt1; structtype4 pkt2;} packetcontent ;//
Unified Packet Data Structure typedef struct tagpacket {byte pkttype; packetcontent pktcontent;} packet;
SummaryIn C/C ++ programming, when multiple basic data types or composite data structures occupy the same piece of memory, we need to use a consortium (Example 1); when there are multiple types and objects, multiple things only take their moments (we call them "N 1 1" in plain words). We can also use a consortium to give full play to its strengths (Example 2 is this ).