Analyze compilation conditions to generate struct memory byte Layout

Source: Internet
Author: User

1. Test Environment

Operating System: win7 (x86 ),

Number of digits: 32

Compiler: C1 (VC ++)

Memory byte sequence: Small endian

2. Alignment rules

The memory layout of the struct depends on the CPU, operating system, compiler, and alignment options during compilation. The alignment requirements of the internal members of the struct, And the alignment requirements of the struct itself. There are three most important points:

1) member alignment. For internal members of a struct, it is generally stipulated that the offset of the starting address of each member variable to the starting address of the structure must be a multiple of the bytes occupied by the type of the variable. However, we can also see that sometimes some fields are closely arranged by size and cannot achieve this goal. Therefore, sometimes padding is required. When each member variable is stored, space is requested in sequence based on the order in which the structure appears, and the position is adjusted according to the alignment above. The vacant byte compiler will automatically fill in the space, that is, padding.

2) then, consider the alignment requirements of the entire structure. The ansi c standard specifies that the alignment requirement of the struct type cannot be looser than the strictest one in all its fields and can be stricter. In fact, the structure must be at least an integer multiple of the largest element in the structure. Sometimes we use a struct array, so the size of the struct must ensure that each struct In the struct array meets the alignment requirements, at the same time, the size of an independent struct should be the same as that of a single struct In the struct array.

3) alignment commands of the compiler. # Pragma pack (n) is provided in VC to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of N, and the default alignment is not required. The total size of the structure also has a constraint, which is divided into the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the largest variable.

3. Why memory alignment?

Http://www.ibm.com/developerworks/library/pa-dalign/

Because the processor reads and writes data, not in bytes, but in blocks (2, 4, 8, 16 bytes. If the alignment is not performed, only one access is required. It may take several times to complete the operation, and additional merging or data separation is required. Low efficiency. More seriously, an error is reported because the CPU does not allow access to the unaligned address, or the debugger or dump core is enabled. For example, Sun sparcsolaris will never tolerate access to the unalignedaddress, the execution of your program will end with a core. Therefore, the compiler will optimize the program during compilation to ensure that all data is stored in 'aligned
Address, which is the origin of memory alignment.

In the 'data alignment: straighten up and fly right' article, the author also concluded that: "If the accessed address is unaligned, using Large-granularity access memory may be slower than small-granularity access memory ".

4. Test code

struct Test{    char a;    int b;    short c;    double d;};int main(int argc, char* argv[]){    int m = 0;    printf("&m=[%08x]\n", &m);    struct Test t;             printf("sizeof(Test)=%d\n", sizeof(struct Test));    printf("&a=[%08x]\n", &t.a);    printf("&b=[%08x]\n", &t.b);    printf("&c=[%08x]\n", &t.c);    printf("&d=[%08x]\n", &t.d);    int n = 0;    printf("&n=[%08x]\n", &n);}

5. Test Condition 1: struct memberalignment = 8

Sizeof (TEST) = 24.

Explanation:

1), 0 x 10% 1 = 0 (where 1 = sizeof (char), matching alignment, A occupies 1 byte;

2), 0 x 11% 2! = 0 (2 = sizeof (short). If the alignment mode is not met, you must add 3 bytes to change the offset to 4 (matching the alignment mode). B occupies 4 bytes;

3), 0 x 18% 4 = 0 (4 = sizeof (INT), matching alignment, C occupies 2 bytes;

4), 0x1a % 8! = 0 (8 = sizeof (double). If the alignment is not met, you must add 6 bytes to change the offset to 32 (matching the alignment). d occupies 8 bytes.

6. test condition 2: struct memberalignment = 4

Sizeof (TEST) = 20.

Description

Struct Test

{

Char A; // The offset is 0. The alignment is satisfied. A occupies 1 byte;

Int B; // the offset of the next available address is 1, not a multiple of sizeof (INT) = 4, the offset can be changed to 4 (the alignment is satisfied) only when three bytes are supplemented. Therefore, VC automatically fills 3 bytes, and // B is stored on the address with the offset of 4, it occupies 4 bytes.

Short C; // the offset of the next available address is 8, which is a multiple of sizeof (short) = 2 and meets the short // alignment mode. Therefore, VC does not need to be automatically filled, C is stored on an address with an offset of 8. It occupies two bytes.

Double D; // the offset of the next available address is 10, not a multiple of sizeof (double) = 8, the offset must be supplemented with/6 bytes to change to 16 (the alignment is satisfied). Therefore, VC automatically fills in 6 bytes, and // D is stored on the address with the offset of 16, it occupies 8 bytes.

};

Therefore, the total size of this structure is: sizeof (test) is 1 + 3 + 2 + 2 + 4 + 8 = 20. Among them, 3 + 2 = 5 bytes are automatically filled by VC, and nothing makes sense.

 

7. Summary

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.