C-language union and large-end and small-end

Source: Internet
Author: User

C-language union and large-end and small-end

The usage of the union keyword is very similar to that of struct.

Union maintains enough space to place one of multiple data members, instead of configuring space for each data member. All data members in union share one space, only one data member can be stored at a time. All data members have the same starting address. Example:

union StateMachine{   char character;   int number;   char *str;   double exp;};
** A union can only be configured with a large enough space to accommodate the maximum length of data members **
In the preceding example, the maximum length is the double type, so the space size of StateMachine is the size of the double data type.

In C ++, the default property page of union members is public. Union is mainly used to compress space.

If some data cannot be used at the same time, you can use union.

I. Influence of the size-end mode on union data

Let's look at another example:

union{   int i;   char a[2];}*p, u;p =&u;p->a[0] = 0x39;p->a[1] = 0x38;

What is the p. I value?

Here we need to consider the storage mode: Big-end mode and small-end mode.
  • Big-end mode (Big_endian): the high bytes of word data are stored in the low address, while the low bytes of word data are stored in the high address.

    The output is 0x39380000.
  • Little_endian: the high bytes of word data are stored in the high address, while the low bytes of word data are stored in the low address.

    The output is 0x00003839.
    The space occupied by union data is equal to the space occupied by the largest member. The access to members of the union type starts from the offset of 0 to the base address of the consortium, that is, the access to the consortium starts from the first address of the union regardless of the variable. Is there an answer to the above question?

    2. How can I use a program to check the storage mode of the current system?

    The above problem seems to be relatively simple, so there is technical content:

    • Please write a C function. If the processor is Big_endian, 0 is returned; If Little_endian, 1 is returned.

      First, according to the above definition of the size-end mode, assume that the int type variable I is initialized to 1.

      • The memory layout of the storage is as follows:

        • It is stored in Small-end mode, and its memory layout is as follows:

          Variable I occupies 4 bytes, but only one byte value is 1, and the value of the other three bytes is 0. If the value of the low address is 0, there is no doubt that this is the big end mode; if the value of the low address is 1, there is no doubt that this is the small end mode. In this case, we can fully utilize the characteristics of the union data: the starting addresses of all Members are consistent.

          Now, how should I write it? The answer is as follows:

          int checkSystem( ){   union check   {      int i;      char ch;   } c;   c.i = 1;   return (c.ch ==1);}

          Now you can use this function to test the storage mode of your current system. Of course, you can also directly view the memory without using a function to determine the storage mode of the current system. For example:


          The value 0x01 in the figure has a low address, indicating that the current system is in the small-end mode.

          However, some systems may support both storage modes. You can use hardware jumpers or set the storage mode in compiler options.

          Leave a question: what is the output value in the x86 system? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> #include intmain(){ int a[5]={1,2,3,4,5}; int *ptr1=(int *)(&a+1); int *ptr2=(int *)((int)a+1); printf("%x,%x",ptr1[-1],*ptr2); return 0;}

          Answer: * ptr1 should be easier to judge.

          Sizeof (a) = 20 a [5] is 4*5 = 20 bytes
          The memory content is:

          a                                                        ptr[-1]   &a+101 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00        00

          & A + 1 indicates that 20 bytes are added at a time, and ptr1 [-1] = offsets one byte at a time. Therefore, ptr [-1] = 5

          (Int) What is the difference between a + 1 and & a + 1? note that very large (int) a + 1 means to change the address of a to an integer and then add 1, here, the offset address only logically adds a unit.

          a (int)a+1                                                  &a+101 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 00

          At this time, * ptr2 points to four bytes 00 00 02 because the computer adopts the small-end mode to forcibly convert 00 00 02 to int *, it becomes 02 00 00 00 00.
          Note: 00 00 00 02 the address here increases sequentially, so 02 is located at the high address

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.