Some Comments on the union structure

Source: Internet
Author: User

# Include "stdio. H"
Typedef Union
{
Char A [2];
Int I;
} ABC;
Void main ()
{
ABC in;
Int x1, x2;
Char alx, ahx;/* alx is the value of the Al register, and ahx is the value of the ah register */
In. I = 0x128;

Printf ("/Na [0] = 0x % x", in. A [0]); // A [0] = 0x28;
Printf ("/nin. I = % x", in. I );
Printf ("/Na [1] = 0x % x", in. A [1]); // A [1] = 0x1;
X1 = in. I;
Printf ("/Nx1 = 0x % x", X1); // X1 = 128

Printf ("/NaX = x2 = 0x % x", X2); // AX = x2 = 0 xcccccccc

Printf ("/nal = 0x % x", alx); // Al = 0 xffffffcc

Printf ("/Hannah = 0x % x", ahx); // Ah = 0 xffffffcc

}

 

A consortium is the memory space shared by all members.

 

First, we need to understand that the byte order of memory storage on different platforms is different. It is compatible with x86 platforms (Intel, AMD, and IBM) is stored/read from low bytes to high bytes, called Little-Endian.
For example:
Int A = 0x0a0b0c0d stored in memory:

Low --> 0d
0c
0b
High --> 0a

On a non-x86 Platform (MAC, IBM non-compatible machine, Motorola), It stores/reads data from high bytes to low bytes, called Big-Endian.
For example:

High --> 0d
0c
0b
Low --> 0a

 

 

In Windows, Int Is 32-bit, that is, 4 bytes, and char a [2] occupies only two bytes, because some four bytes of the int type are used to allocate space.

In. I = 0x128
In fact, I is 0x00000128, sorting 0x28 0x01 0x00 0x00 in the memory
In the Union you define, A is a string of 2 characters.

The reason why X2 outputs 0xcccccccc is that you have not initialized X2, And the uninitialized stack space in the debug version program of VC is CC...

The same is true for alx and ahx, that is, 0xcc. Why do they output 0xffffcc?
This is because you output alx and ahx as Int. The input parameter printf implicitly converts it to int.
Because char is also signed, its symbol bit is extended to the high byte, that is, 0xffffffcc.

The program changes are as follows:
In. I = 0x10111213;

Printf ("/Na [0] = 0x % x & A [0] = 0x % x", in. A [0], & in. A [0]);
Printf ("/Na [1] = 0x % x & A [1] = 0x % x", in. A [1], & in. A [1]);
Printf ("/Na [2] = 0x % x & A [2] = 0x % x", in. A [2], & in. A [2]);
Printf ("/Na [3] = 0x % x & A [3] = 0x % x", in. A [3], & in. A [3]);
Printf ("/nin. I = 0x % x & in. I = 0x % x", in. I, & in. I );

Of course, a [2] and a [3] have crossed the border, but they are within their own control. The result is as follows:

A [0] = 0x13 & A [0] = 0x22ff8c
A [1] = 0x12 & A [1] = 0x22ff8d
A [2] = 0x11 & A [2] = 0x22ff8e
A [3] = 0x10 & A [3] = 0x22ff8f
In. I = 0x10111213 & in. I = 0x22ff8c

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.