Talk C Chestnut Bar (152th: C-language instance-the size end of the computer)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about socket communication address example, this time we say the example is: the size of the machine in the end . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

The size of the end is said to originate from the novel Gulliver's Travels, the book said: "People are arguing about the way to open eggs from the big end of the egg or the small side open the egg." Debate, and even caused the war. You say, no matter where you open the eggs, open eggs, what can be argued? and to be solved by war. You do not have to pay attention to this argument, if you really want to know the details, you can look at the novel. The size of the computer, however, is not as simple as arguing about opening an egg in a novel because it has a different meaning.

the size end represents the order in which bytes are stored on the computer. it was translated from the English Big endian and Little endian . Most of the data we use is stored in more than one byte, and the order of these bytes is arranged in a certain sequence:

    • When a computer stores data using a big-endian way, the order of the bytes is from high to low.
    • When a computer uses a small-end way to store data, the order of the bytes is from low to high.

So, you might think it's very abstract, and then we'll use concrete examples to illustrate what the size side is. For example: A value in computer memory: 0x12345678. 1 means high and 8 is low.

    • When the computer uses the big-endian to store the data, the data is stored in the computer as: 0x12345678.
    • When the computer uses a small end to store the data, the data is stored in the computer as: 0x87654321.

At this time there are crossing asked: we usually write programs do not realize the size of the end of what is different? In fact, this is not to blame, because most of the personal computers we currently use are computers with X86 architectures, and computers of that type use small-end methods to store data. This means that everyone is using the same way of storing data, so the difference in data storage is not realized. On some large servers, computers that store data in the big-endian way, which we rarely have access to. We can use code to judge if we are not sure which of the computers we use belongs to the size side.

 #include <stdio.h>  int  Main () {int  a = 0x12345678 ; int  len = sizeof  ( int );    //get the Count of byte     int  i =-0 ;    char  *p = (char  *) &a; printf     (); while     (I<len)        {printf  ( "[%p] [%x] \ n" , p+i,* (p+i));    i++; } return  0 ;}  

In the code we assign the hexadecimal data to an int variable, and the hexadecimal value is used to make it easier to see when outputting the value. We all know that the computer uses multiple bytes to store int variables, using a single byte to store char variables, so we cast the address of the int type variable to a pointer of type char, so that the storage details of the int variable can be viewed in bytes. These details include:

    • The number of bytes of the int variable;
    • The order in which multiple bytes are stored;
    • The data stored in each byte.

The following is the results of the operation of the program, please refer to:

 [address]   [value] [0xbfe7a880]   [78] [0xbfe7a881]   [56] [0xbfe7a882]   [34] [0xbfe7a883]   [12]

As you can see from the program running results above, the int variable uses four bytes to store the data, and the four-byte addresses are arranged sequentially from 0x0xbfe7a880 to 0xbfe7a883, where low address 0xbfe7a880 stores low data 78. High-level address 0xbfe7a883 stores high-level data 12. With the introduction above, you can see that the computer uses a small-end way to store data.

Crossing, in addition to some of the big-endian computers, the use of sockets in network communications to use the large-endian transfer of data, which is also our introduction of sockets in the process of specifically inserting a chapter back to introduce the size of the end of the reason. As we all know, our computers use small-end methods to store data, and if you use network communication, you need to use the big-endian way to transfer data. This is obviously different, how to deal with the difference in storage mode? We will do a detailed introduction in the later chapters.

You crossing, the example of the size and the end of a computer. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (152th: C-language instance-the size end of the computer)

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.