Memory alignment and memory alignment principles

Source: Internet
Author: User

Memory alignment and memory alignment principles

# Pragma pack (8)

Struct TestStruct4 {

Char;

Long B;

};
Struct TestStruct5 {

Char c;

TestStruct4 d;

Long e;

};

# Pragma pack ()

Question: A), sizeof (TestStruct5) =?

B) How many bytes are left behind c in TestStruct5, followed by d?

In TestStruct4, member a is 1 byte, which is aligned by 1 byte by default. The alignment parameter is 8. Among the two values, 1 and a are aligned by 1 byte. Member B is 4 bytes, the default value is 4-byte alignment. In this case, it is 4-byte alignment, so sizeof (TestStruct4) should be 8;

In TestStruct5, c is the same as a in TestStruct4. It is aligned by 1 byte, while d is a structure. It is 8 bytes. What is its alignment? For the structure, its default alignment is the largest of all its members using alignment parameters. TestStruct4 is 4. Therefore, member d is aligned in 4 bytes. The member e is 8 bytes, which is aligned by 8 bytes by default. It is the same as the specified one, so it is connected to the boundary of 8 bytes. At this time, 12 bytes are used, therefore, four bytes of null are added, and member e is placed from 16th bytes. At this time, the length is 24 and can be divisible by 8 (member e is aligned by 8 bytes. In this way, a total of 24 bytes are used. The memory layout is as follows (* Indicates idle memory, and 1 indicates memory usage. Unit: 1 byete ):

A B

Memory layout of TestStruct4: 1 ***, 1111,

C d (TestStruct4.a TestStruct4. B) e

Memory layout of TestStruct5: 1 ***, 1 ***, 1111, ***, 11111111

There are three important points:
First, each member is aligned in its own way, and the length can be minimized. Secondly, the default alignment of complex types (such as structures) is the alignment of its longest member, so that the length can be minimized when the member is a complex type. Then, the length after alignment must be an integer multiple of the largest alignment parameter in the member, so that each item can be aligned when processing the array.

In addition, the alignment of arrays, such as char a [3]; is the same as that of the three char Types respectively. That is to say, it is still aligned by 1 byte. If you write: typedef char Array3 [3]; the alignment type of Array3 is still aligned by 1 byte, rather than by its length.

However, no matter what the type is, the alignment boundary must be 1, 2, 4, 8, 16, 32, 64.

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.