About some csapp reads (1) -- alignment (from machine-level representation of Program)

Source: Internet
Author: User

The sequence of variable definition isProgramHow much is the impact of running efficiency. I used to be naive. During the summer vacation, I started to read the csapp, which is similar to the computer architecture, but it was written from the programming perspective. Now I am still struggling with the photographic version, it took more than a week to finally read the chapter "machine-level representation of program", with only one left in the head... GCC... And assembly language... Khan .. However, the compiler is really powerful, which directly causes us to writeCodeRegardless of the habits of underlying operations.

I found it only today when I read data alignment .. When different variables are defined in struct, the order is usually different, resulting in different memory allocations, and the offset positions of each internal variable are also different. The Linux and Windows regulations are different here.

Limit alignment is designed to simplify the design of hardware interfaces between storage and CPU.

In Linux, a small alignment type is a multiple of 2, that is, a type such as short. The offset must start from a multiple of 2, the offset position of other large variables must start from a multiple of 4. For example:

 
Struct mixeddata {int data1; short data2; int data3 ;};

The offset of data1 is 0, the offset of data2 is 4, and the offset of data3 is 8. When GCC translates this code, it inserts 2 bytes of space between data2 and data3 to meet the Linux offset limit. If this struct is defined

 
Struct mixeddata {int data1; int data3; short data2 ;};

The allocated memory space is totally different from the previous struct.

In Windows, such restrictions are more stringent. Basically, if the size of a type is M byte, the offset of this type of variable in the struct can only be a multiple of M, that is, the offset position of N * m. Therefore, the variable sequence defined in the struct in win results in a lot of memory waste.

Well .. In fact, this chapter also has a more important part in the compilation language of the processing of various processors in the system during recursive calling... But this is quite common...

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.