Union type data storage and calculation

Source: Internet
Author: User

Today, when I was doing a C-language topic, I met a question like this:

#include <iostream>usingnamespacestd;    union    {        int i;        char x[2];    }a;int main(){    a.x[010;    a.x[11;    cout<<a.i;    system("pause");    return0;}

The result is this:

Start my idea is that int accounts for four bytes, x[0],x[1] A byte respectively, because the inertial thought x[0] is in front of x[1], int first byte corresponds to the previous byte x[0], the second byte corresponds to x[1], so the result is 00001010 00000001 is 2561, but in fact it is 00000001 00001010, that is, 256 + 10 = 266, I am also baffled its solution, behind looking for all kinds of information, finally found the mystery.

As we all know, C language is divided into many versions, of which more is C80 and C99 two versions, now we use basically are C99, here to C99 as the standard. (int is two bytes in C80 and 4 bytes in C99)

Through access to information, learned that the computer is divided into big and small end of the two ways of storage, mainly related to the internal instructions of the CPU, then the following specific talk about the difference between the two storage methods.

big-endian mode and small-end mode.
Big- endian mode (Big_endian): The high byte of the character data is stored in the low address, while the low byte of the word data is placed in the high address.
Small End Mode (Little_endian): The high byte of the character data is stored in the high address, while the low byte of the word data is stored in the low address.
The space occupied by the Union data is equal to the space occupied by its largest member, of course, this sentence is applicable on the C80, but it is not applicable on the C99, C99 regardless of the struct and the Union type to consider the case of memory alignment, here does not elaborate. The access to the union member is starting at 0 from the base address of the consortium, that is, the access of the consortium, regardless of which variable is accessed, starts at the first location of the union.

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

stored in big-endian mode with memory layouts such as:

Storage in small-end mode, with memory layouts such as:

Then we can judge by the characteristics of big and small ends.
Array elements are always arranged from the ground address to the high address, which has no effect on the big or small end of the CPU, so for the long-endian pattern, the character array corresponds to the int data memory:

Small-end mode:

In general, the CPU is basically used in the small-end mode, which is the second counterpart, so the calculation method has changed.

Second, we can determine whether a machine's CPU is in the big-endian or the small-end mode by the equality of the first element of the type int's low and the character array:
Here is the beginning of the Union program, namely: i = = a.x[0] to judge.

#include <iostream>using namespace STD;Union{intICharx[2];} AintMain () {a.x[0] =1; a.x[1] =0;if(A.I = = a.x[0])cout<<"Little"<<endl;Else        cout<<"Big"<<endl;return 0;}

Union type data storage and calculation

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.