The principle of member variable alignment

Source: Internet
Author: User

It took me a morning to read some information and finally got the problem right. Let me show you some examples of alignment issues for struct member variables.   for struct S1{ char a; long int d;double c;};   The size of this struct is 16. The compiler's default is generally 8-byte alignment. The size of a is 1, it is 1-byte alignment (because it is smaller than the specified 8), stored in the 0 offset, b size is 4, it is aligned with 4 bytes (because it is smaller than the specified 8), there is offset 4--7 position, C size 8, there is 8--15 position. This allows 3 members to occupy a total of 16 bytes. Since the maximum member C size of the structure is 8, the structure is aligned by 8 bytes, 16 by 8 Park or 16, so sizeof S1 = 16.  and for  struct s2 { char A; long int D;    double C; char e; };  The size of this structure is 24. The first 3 members are stored as above, D is in the 4--7 position, C is in the 8--15 position, but e is aligned by 1 bytes, there is an offset position of 16, so 4 members occupy 17 bytes. Since the size of the largest data member C of the structure is 8 bytes, 17 to 8 yuan is 24.     Of course you can use the #pragma directive to specify that the compiler aligns by 4 bytes. i.e.   #pragma pack (4)       //This can also be #pragma pack (push,4)    struct s1  { char A; long int d; double c; };   struct S2 { char a;  Long int d; double C; char e; };    Then the size of S1 is 16, and the size of S2 becomes 20. Let's analyze that, for S1, the position of 4-byte alignment and 8-byte alignment of data members is the same, except that the last one, 16 to 4 yuan or 16. The S2 is not the same, the size of a is 1 (smaller than the specified 4-byte alignment), 1-byte alignment, stored in 0 position, D is 4 (greater than or equal to the specified 4 bytes), by 4 byte alignment, stored in the 4--7 position, C is 8 (greater than the specified 4 wordssection), by 4-byte alignment, so that the size of the 8--15,e is 1, stored in position 16, so that the length of the entire structure is 17,17 to 4 yuan whole, 20. As you can see, it is not specified that the compiler is aligned 4 bytes in 4-byte alignment. For example, the following structural body:  #pragma pack (4)  struct teststruct2 {     Char m1[11];      Short m2; };  Do you know the size of it? is 14. Because the M1 is aligned in 1 bytes, it is stored in the 0--11 position, and M2 is aligned in 2 bytes and stored in the 12--13 position. The struct occupies 13 bytes, because the data type of the largest member in the structure is short, the size is 2, which is smaller than the specified alignment byte 4, so 13 to 2 yuan, 14. All in all, the alignment of a struct member is aligned with the size of the member itself and the small number of n in the #pragma pack (push,n), for example, if the member size is 2 and the alignment you specify is 4, the member is aligned by 2, and the structure itself is the size of the largest member in the structure and # pragma pack (push,n) is a small number of N-aligned, that is, the final rounding, for example, if the maximum member size in the structure is 8, and you specify that the alignment is 16, the structure itself is aligned by 8.  

The following gives the number of bytes for the base data type under the different bits compiler:


16-bit compilers


Char:1 bytes
char* (i.e. pointer variable): 2 bytes
Short Int:2 bytes
Int:2 bytes
Unsigned int:2 bytes
Float:4 bytes
Double:8 bytes
Long:4 bytes
A long long:8 bytes
Unsigned long:4 bytes


32-bit compilers


Char:1 bytes
char* (i.e. pointer variable): 4 bytes (32-bit addressing space is 2^32, which is 32 bit, or 4 bytes. Similarly 64-bit compilers)
Short Int:2 bytes
Int:4 bytes
Unsigned int:4 bytes
Float:4 bytes
Double:8 bytes
Long:4 bytes
A long long:8 bytes
Unsigned long:4 bytes


64-bit compilers

Char:1 bytes
char* (i.e. pointer variable): 8 bytes
Short Int:2 bytes
Int:4 bytes
Unsigned int:4 bytes
Float:4 bytes
Double:8 bytes
Long:8 bytes
A long long:8 bytes
Unsigned long:8 bytes

The principle of member variable 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.