[Original] Alignment a little bit to understand

Source: Internet
Author: User

Compile the CompilerProgramTime alignment.

32Bit C ++Use default 8To improve the running efficiency, the compiler tries its best to put the data in the field to improve the memory hit rate. That is, suppose there is an intType data, int4Bytes (if no bounds is specified, the default bounds 8 is used.). If the value is smaller than the default peer interface, the value is 4., The intType data should be stored in memory address 4To facilitate searching. In fact, the compiler will allocate a certain amount of space for the variables in the memory. Their placement is not compact and put together one by one, because this is not conducive to the compiler efficiency. The example of structured storage is as follows, in the memory, the storage of variables alignment with the largest number of bytes of the variables. Why is the maximum alignment required? A struct:

Type struct
{
Int A;
Char C;
Short S;
} Teststruct;

 

Imagine if S is usedThe two bytes of the variable are aligned, which causes intTypeIt cannot be stored in the byte group where the compiler accesses data at a time. It is forced to be allocated in different byte arrays.

during alignment, if the variable cannot be stored in a byte array, it is automatically stored in the next byte array. In fact, alignment is not our job, it is compiled, but if we can understand alignment well, the Program Code can be better optimized. The benefits are also described in the previous article . http://www.cnblogs.com/IamEasy_Man/archive/2009/12/13/1623187.html

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.