Sizeof byte alignment

Source: Internet
Author: User

(1) starting address of each member % alignment value of each member = 0. If the value is not equal to 0, the byte is null until the expression is true;
(2) The overall length of a struct must be an integer multiple of its own alignment values. If it is not enough, the byte is left blank.

 

# Pragma pack (8)

Struct {

Char;

Long B;

};

For struct a, its own alignment value is 1 for char data, and for long data, its own alignment value is 4, the maximum alignment value of a struct is 4. The sequential steps of struct a in memory are as follows:

(1) Char A. The address range is 0x0000 ~ 0x0000. The starting address is 0x0000, which must be 0 x 0000% 1 = 0. The member bytes are aligned.

(2) Long B, the starting position of the address cannot start from 0x00001, because 0 x 0001% 4! = 0, so first fill in the Null Byte until the end of 0x00003, that is, fill in 3 bytes of Null Byte, from 0x00004 to store B, its address range is 0x00004 ~ 0x0007.

(3) At this time, all members are stored and ended. The length of the struct is 8, which is twice the alignment value of the struct itself. The conditions are met (2 ).

In this case, condition (1) and condition (2) are met. The positions of each member in struct a in the memory are: A *** B, and sizeof (struct a) = 8. (Each asterisk represents one digit, and each member represents its own position. For example, a occupies one digit and B occupies four digits)

 

 

Struct B {

Char;

Struct a B;

Long C;

};

For struct B, there is a member B of the type struct a with its own alignment value of 4. For the long type, its own alignment value is 4. therefore, the self-alignment value of struct B is 4. The sequential steps of struct B in memory are as follows:

(1) Char A. The address range is 0x0000 ~ 0x0000. The starting address is 0x0000, which must be 0 x 0000% 1 = 0. The member bytes are aligned.

(2) struct a B, the starting position of the address cannot start from 0x00001, because 0 x 0001% 4! = 0, so first fill in the Null Byte until the end of 0x00003, that is, fill in 3 bytes of Null Byte, from 0x00004 to store B, its address range is 0x00004 ~ 0x00011.

(3) For long C, the starting position of the address starts from 0x000012. Because 0 x 0012% 4 = 0, the address range is 0x00012 ~ 0x0015.

(4) At this time, all members are stored and ended. The length of the struct is 16, which is four times the alignment value of the struct itself. The conditions are met (2 ).

In this case, conditions (1) and conditions (2) are met. The positions of each member in struct B in the memory are: A *** B C, sizeof (struct c) = 24. (Each asterisk represents one digit, and each member represents its own position. For example, a occupies one digit, B occupies eight digits, and C occupies four digits)

 

Struct c {

Char;

Struct a B;

Double C;

};

For struct C, there is a member B of the type struct a with its own alignment value of 4. For the double type, its own alignment value is 8. therefore, the self-alignment value of struct C is 8. The sequential steps of struct C in memory are as follows:

(1) Char A. The address range is 0x0000 ~ 0x0000. The starting address is 0x0000, which must be 0 x 0000% 1 = 0. The member bytes are aligned.

(2) struct a B, the starting position of the address cannot start from 0x00001, because 0 x 0001% 4! = 0, so first fill in the Null Byte until the end of 0x00003, that is, fill in 3 bytes of Null Byte, from 0x00004 to store B, its address range is 0x00004 ~ 0x00011.

(3) Double C, the address start position cannot start from 0x000012, because 0 x 0012% 8! = 0, so first fill in the Null Byte until the end of 0x000015, that is, fill in 4 bytes of Null Byte, from 0x00016 to store c, its address range is 0x00016 ~ 0x0023.

(4) At this time, all members are stored and ended. The length of the struct is 24, which is three times the alignment value of the struct itself. The conditions are met (2 ).

In this case, conditions (1) and conditions (2) are met. The positions of each member in struct C in the memory are as follows: a *** B *** C and sizeof (struct C) = 24. (Each asterisk represents one digit, and each member represents its own position. For example, a occupies one digit, B occupies eight digits, and C occupies eight digits)

 

 

Struct d {

Char;

Struct a B;

Double C;

Int D;

};

For struct D, the alignment value is 8. The first three members are consistent with struct C. For the fourth member D, because 0 x 0024% 4 = 0, it can be stored from 0x0024, and its address range is 0x00024 ~ 0x00027. At this time, all members are stored and ended. The length of the struct is 28, and 28 is not a multiple of the alignment value 8 of the struct. Therefore, four spaces must be added after the struct, that is, at 0x0028 ~ Fill in four spaces on 0x0031. After completion, the struct length is 32

 

4 of the alignment values, meeting the condition (2 ).

In this case, condition (1) and condition (2) are met. The position of each member in struct d in the memory is a *** B *** c d ****, sizeof (struct d) = 32. (Each asterisk represents one digit, and each member represents its own position. For example, a occupies one digit, B occupies eight places, C occupies eight places, and D occupies four places ).

 

Struct E {

Char;

Int B;

Struct a C;

Double D;

};

 

Typedefstruct node3

{

Int;

Short B;

Static int C;

} S3;

Sizeof (S3) = 8. Here, the struct contains static data members, and the storage location of static data members is irrelevant to the storage address of the struct instance.

 

 

 

 

Sizeof byte alignment

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.