Reading C language deep anatomy notes: large and small

Source: Internet
Author: User

Who said there is no domesticA good book for Nb people and Nb!《Deep anatomy of c LanguageIn the third line, I wrote, "Bravely challenge similar books at home and abroad with gold "!!

The content was mentioned when the author talked about Union. At that time, I did not really understand it. I only knew that there were two storage modes. The concept is as follows:

Big-end mode: 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.

Small-end mode: 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.

We also know that there is a way to select the storage mode of the system and two diagrams:

 
/*0, 1*/
IntChecksystem ()
{
Union check
{
IntI;
CharCh;
} C;
C. I =1;
Return(C. CH =1);
}

 

Then I kept looking at the question: what is the value of the x86 system?

 
IntMain ()
{
IntA [4] = {1,2,3,4};
Int* Ptr1 = (Int*) (& A +1);
Int* Ptr2 = (Int*)((Int) A +1);

Print ("% X, % x", Prt1 [-1], * Ptr2 );

Return 0;
}

Ptr1 first, I know that & A is the first address of the entire array, so & A + 1 refers to the end of the entire array

Because the address (& A + 1) is converted to (int *), (A & + 1) points to the integer of the int type after the entire array. Prt1 [-1] is * (prt1-1), that is a [3], so it is 4, this made by yourself!

Let's look at ptr2. First, I know that a represents the first address of the array's first element. (INT) A + 1 is to convert address a to the int type and Add 1 because the int type is 4 bytes of storage, (INT) A + 1 points to the second byte pointer of a [0. Ptr2 converted to (int *) points to an int type as it represents. I also thought about it. What is the value that ptr2 points? Here, it is related to the storage mode!

According to my understanding, I can know that a [0] is [0x00 0x00 0x00 0x01 ], A [1] is 0x00 0x00 0x00 0x02. In this way, I don't know what kind of storage this is. I don't want to watch it during these two days, I gave up. How can I think of this mentality ?!! Not me that year ~, It was a small problem, so I couldn't look down on myself so much. So I went to the front to look at it in detail and explain it again. It turned out that it was so easy and clear to calm down! The following is a concise illustration in the editor, and it is easy to remember:

Is that clear!The front and back of the small terminal.(Note: Some systems have both modes .)

* The ptr2 value is also revealed, with the large end 0x100 and the small end 0x2000000.

 

So,Learning Needs to be calm!

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.