[to] talk about the use of a consortium (union) from two classical questions in C + +

Source: Internet
Author: User

Song Baohua 21cnbao [email protected] Question 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 person, we should be in the computer composition have learned what is called little endian and big endian. Little endian and big endian are two different sequences of CPU storage data. For data types such as Integer, Long Integer, Big endian thinks the first byte is the highest byte (the high byte to the low byte of the data from the low address to the high address), and the little endian the opposite, It considers the first byte to be the lowest byte (the low byte to the high byte) that holds the data in the order from the lower address to the higher address. For example, suppose you start with the following data from the memory address 0x0000:
0x0000 0x0001 0x0002 0x0003
0x12 0x34 0xAB 0xCD
If we go to read a four-byte variable with an address of 0x0000, if the byte order is Big-endian, the result is read 0X1234ABCD, and if the byte-order bit Little-endian, the result is read 0xcdab3412. If we write 0X1234ABCD to memory starting with 0x0000, the results of little endian and 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, x86 series CPUs are Little-endian byte-order, PowerPC is usually big endian, and some CPUs can set the CPU to work in little endian or big endian mode via jumpers. Answer:Obviously, the solution to this problem can only be a byte (char/byte type) of data and an integer data stored in the same memory start address, by reading the integer data, analyzing the Char/byte data in the integer data high or low to determine the CPU working in little Endian is still the big endian mode. The following answer is obtained: 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 the CPU is big\n");      }        return 0;} In addition to the methods described above (casting by pointer type and assigning values to the first byte of integer data, the assignment is judged to be assigned to theHigh or low), is there a better way? We know that the members of the union themselves are stored in the same memory space (shared memory, where the Union plays a role and contribute), so we can combine a char/byte data and an integer data as a union member, and get 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 relevant source code in the Linux operating system is done: static Union {char c[4]; unsigned long l;} endian_test = {{' L ', '? ', '? ', ' B '}} ; #define ENDIANNESS (char) endian_test.l) Linux kernel authors implement a large piece of code with just one union variable and a simple macro definition! From the above piece of code we can deeply understand the subtlety of the Linux source code! question two:It is assumed that the communication Protocol in Network node A and network Node B involves four kinds of messages, the message format is "The structure of Message Type field + message content", and the structure type of four message content is structtype1~ STRUCTTYPE4 respectively. Please write a program to organize a unified message data structure in the simplest way. Analysis:The format of the message is "message type + message content structure", in the real communication, can only send one of four kinds of messages, we can be the structure of the four class of messages organized into a union (share a memory, but each effective is only one), and then unified with the message Type field into a message data structure. Answer:According to 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 message data structure typedef struct tagpacket{BYTE PktType; Packetcontent pktcontent;} Packet; SummaryIn the writing of C + + programs, when multiple basic data types or composite data structures occupy the same piece of memory, we use a consortium (an example of question one); When multiple types, multiple objects, and many things take only one (let's call it "N-Select 1"), We can also use a consortium to make the most of its strengths (question two is such an example).

This article is from the "Song Baohua blog" blog, make sure to keep this source http://21cnbao.blog.51cto.com/109393/120108

[to] talk about the use of a consortium (union) from two classical questions in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.