Sizeof + structure memory alignment

Source: Internet
Author: User
Tags modulus microsoft c
Sometimes, the "obvious" things that have been paused for a long time in my mind are basically wrong. Let's take a look at the following questions: What kind of answer will struct t {char ch; int I ;}; what kind of answer will be obtained when sizeof (t) is used? If you do not want to think about it before, in 32-bit machines, Int Is 4 bytes, char is 1 byte, so T is a total of 5 bytes. In practice, the answer is actually 8 bytes tested in vc6. Well, I am always hurt, and I am a little numb. Please accept it honestly! Why is the answer different from what you think? The memory alignment concept will be introduced here. Many real computer systems have limits on the locations where basic data is stored in the memory. They require that the first address value of the data be K (usually 4 or 8) this is the memory alignment, and this K is called the alignment modulus of the data type ). When the ratio of the alignment modulus of one type of S to the alignment modulus of another type of T is an integer greater than 1, we call it the alignment requirement of type s stronger than that of T (strict ), t is weaker (loose) than S ). This mandatory requirement simplifies the design of the transmission system between the processor and the memory, and improves the Data Reading speed. For example, a processor reads or writes 8 bytes of data at a time starting from an eight-fold address each time it reads/writes memory, if the software can ensure that data of the double type starts from an eight-fold address, then only one memory operation is required to read or write data of the double type. Otherwise, we may need two memory operations to complete this operation, because the data may be distributed across two 8-byte memory blocks that meet the alignment requirements. Some Processors may encounter errors when the data does not meet the alignment requirements, but Intel's ia32 architecture processor can work correctly regardless of whether the data is aligned. However, Intel recommends that if you want to improve performance, all program data should be aligned as much as possible. The ansi c standard does not stipulate that variables declared adjacent must be adjacent in memory. For program efficiency, memory alignment problems are flexibly handled by the compiler, which may cause some padding bytes between adjacent variables. For the basic data type (INT char), the memory space they occupy has a fixed value in a fixed hardware system. Therefore, we will only consider the memory allocation of struct members. Alignment policy of the Microsoft C compiler (cl.exe for 80 × 86) in win32platform: 1) the first address of the struct variable can be divisible by the size of its widest basic type member; Note: When the compiler opens space for the struct, first, find the widest basic data type in the struct, and then find the location where the memory address can be divisible by the basic data type as the first address of the struct. Use the size of the widest basic data type as the alignment modulus described above. 2) The offset of each member of the struct to the first address of the struct is an integer multiple of the member size. If necessary, the compiler will add the internal adding between the members. Note: before opening a space for a member of the struct, the compiler first checks whether the offset of the first address of the preopened space to the first address of the struct is an integer multiple of the current member. If yes, It stores the Member. Otherwise, then, a certain number of bytes are filled between the current member and the previous Member to reach an integer multiple. That is, the first address of the pre-opened space is removed several bytes. 3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ). Note: The total size of the struct includes the padding byte. The last member must meet the preceding two conditions and the third condition. Otherwise, the last few bytes must be filled to meet the requirements. According to the preceding rules, in windows, the size of sizeof (t) is 8 bytes using the VC compiler. In the GNU gcc compiler, there are some differences in the principles followed. The alignment modulus is not determined based on the widest basic data type as described above. In GCC, the maximum alignment modulus is 4. That is to say, the alignment modulus can only be 1, 2, and 4, even if the structure has a double type. In addition, in the preceding three items, the offset value must be an integer multiple of the member size. If the member size is smaller than or equal to 4, the offset value is calculated according to the preceding rules. If the member size is greater than 4, the offset of each member of the struct to the first address of the struct can only be determined by an integer multiple of 4. Take the following example: struct t {char ch; double D ;}; in GCC, sizeof (t) should be 12 bytes. If the struct contains a bit-field, the guidelines in VC must be changed: 1) if the fields of the adjacent bit-field are of the same type, and the sum of its bit width is smaller than the sizeof size of the type, the subsequent fields will be stored close to the previous field until they cannot be accommodated; 2) if the adjacent fields have the same type, however, if the sum of its bit width is greater than the sizeof size of the type, the following field starts from the new storage unit, and its offset is an integer multiple of its type size; 3) if the types of adjacent bitfield fields are different, the specific implementation of each compiler is different. vc6 adopts the non-compression mode (different bitfield fields are stored in different bitfield types in bytes ), both Dev-C ++ and GCC adopt compression methods. Note: when the two fields have different types, for example, struct n {char C: 2; int I: 4 ;}; the memory alignment criterion for the non-bit domain struct should still be met. the offset of the I member to the first address of the struct should be an integer multiple of 4, therefore, the c Member needs to fill in 3 bytes, and then open up 4 bytes of space as the int type, four of which are used to store I, so the above The space occupied by the struct in VC is 8 bytes. for compilers that adopt compression methods, the memory alignment criterion for the struct without bit fields is 2nd. The difference is that, if the three characters to be filled can save the space of the subsequent members, the data will be compressed to the byte. If the three characters cannot be filled, the space will be opened up separately, therefore, the above struct n occupies 4 bytes in GCC or Dev-C ++. 4) if the fields in the bitfield are interspersed with non-bitfield fields, no compression is performed. Remarks: struct: typedef struct {char C: 2; double I; int C2: 4 ;} n3; the space occupied by GCC is 16 bytes, and the space occupied by VC should be 24 bytes. 5) the total size of the entire struct is an integer multiple of the size of the widest basic type. PS: the choice of alignment modulus can only be based on the basic data type. Therefore, for nested struct In the struct, you can only consider the basic data type for its splitting. For the 2nd records in the alignment criterion, the entire struct is regarded as a member. The size of the member is determined based on the alignment criterion. Class objects are stored in the memory in a similar way as struct, which is not described here. It should be noted that the size of the class object only includes the space occupied by non-static member variables of the class. If there is a virtual function, you can add another space occupied by the pointer.

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.