C/C ++ struct memory alignment

Source: Internet
Author: User
Tags modulus microsoft c
C/C ++ struct memory alignment

Programming Language 13:22:38 read 596 comments 2 font size: LargeMediumSmall subscription

Memory alignment

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) alignment of members. 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. Second, if n is smaller than the number of bytes occupied by the type of the variable, the offset is a multiple of N and does not need to meet the default alignment. 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.

Rules: Http://bigwhite.blogbus.com/logs/1347304.html
1. Data member alignment rules: the data member of the structure (struct) (or union). The first data member is placed at the position where the offset is 0, in the future, the alignment of each data member is performed according to the value specified by # pragma pack and the smaller value of the data member's length.
2. Overall alignment rules of the structure (or union): After the data members align themselves, the structure (or union) itself also needs to be aligned, alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union.
3. Combined with 1 and 2, we can infer that when the n value of # pragma pack is equal to or exceeds the length of all data members, the N value will not have any effect.

Summary:
An important condition for member alignment is that each member is aligned separately. that is, each member is aligned in its own way. if there is # pragma pack (8), although it specifies to be aligned by 8 bytes, not all Members are aligned by 8 bytes. the alignment rule is that each member has a smaller alignment according to the type alignment parameter (usually the size of this type) and the specified alignment parameter (8 bytes here. in addition, the length of the structure must be an integer multiple of all alignment parameters used. that is to say, the length after alignment must be an integer multiple of the largest alignment parameter in the member, so that the boundary alignment of each item can be ensured during Array Processing. In fact, the algorithm for arranging the entire memory layout based on these rules is very simple. Assume that the starting address is 0 and 1st members are placed, and then find the starting position where the next member can be placed, first, this position must be out of the first Member, followed by those alignment factors. Find the first location that meets the two conditions. Next, consider the next member and proceed one by one. Finally, consider the alignment factor of the entire struct to determine the Ending position of the entire struct. The next position is the starting position of the next struct to ensure that it can be aligned.

For example:
Struct mystruct
{
Char DDA;
Double dda1;
Int type
};

(Simple description)
Struct mystruct
{
Char DDA; // The offset is 0. The alignment is satisfied and the DDA occupies 1 byte;
Double dda1; // the offset of the next available address is 1, not sizeof (double) = 8
// Multiple. You need to add 7 bytes to make the offset 8 (the alignment is satisfied ).
// Method). Therefore, VC automatically fills in 7 bytes, and dda1 is stored at an offset of 8
// Address, which occupies 8 bytes.
Int type; // the offset of the next available address is 16, which is twice that of sizeof (INT) = 4.
// Number, which meets the alignment of int, so no VC auto-fill is required.
// Put the address at the offset of 16, which occupies 4 bytes.
}; // All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, not a structure.
// Number of knots (that is, the number of bytes occupied by the largest space type in the structure sizeof
// (Double) = 8), so four bytes need to be filled to meet the structure size
// Sizeof (double) = a multiple of 8.
Therefore, the total size of this structure is: sizeof (mystruc) is 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, and nothing makes sense.

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 merger 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 you can open the debugger or dump the core. For example, you will never be allowed to access the unaligned address, 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 on 'aligned address' during the program running. This 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 ".

Bit domain

Http://www.ksarea.com/articles/20071004_sizeof-struct-memory.html

If the struct contains bit-field, the guidelines in VC must be changed:
1) if the types of adjacent fields are the same, and the sum of the bit widths is smaller than the sizeof size of the type, the subsequent fields will be stored next to the previous field until they cannot be accommodated;
2) If the Field Types of adjacent bit fields are the same, but the sum of Bit Width is greater than the sizeof size of the type, the subsequent fields start from the new storage unit, its offset is an integer multiple of its type;
3) if the types of adjacent bitfield fields are different, the specific implementation of each compiler varies, vc6 adopts the non-compression mode (the fields of different bit domains are stored in different bit domain type bytes), and both Dev-C ++ and GCC adopt the compression mode;
Note: When the two fields are of different types, for example:

Struct n
{
Char C: 2;
Int I: 4;
};
The memory alignment criteria for the non-bit domain struct are still 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 must be filled with three bytes, then the space of four bytes is opened up as the int type, four of which are used to store I, so the space occupied by the above struct in VC is 8 bytes; for compilers that adopt compression, the memory alignment criteria of the non-bit domain structure are followed. The difference is that if the three words are filled with energy saving, the data is compressed to the padding byte, which cannot be accommodated. Therefore, the space occupied by the above struct N in GCC or Dev-C ++ should be 4 bytes.

4) do not compress fields that are interspersed with non-bit fields;
Note:
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 is 24 bytes.
5) the total size of the entire struct is an integer multiple of the size of the widest basic type.

See a reference
######################################## ##########################
Http://blog.csdn.net/manbug/archive/2006/08/26/1124845.aspx
First, at least one thing is certain, that is, ansi c ensures that the locations where fields in the struct appear in the memory increase sequentially with their Declaration Order, and the first address of the first field is equal to the first address of the entire struct instance. At this time, a friend may ask: "Does the standard stipulate that adjacent fields are also adjacent in the memory? ". Well, sorry, ansi c does not guarantee that your program should not rely on this assumption at any time. Does this mean that we can never outline a clearer and more precise structure memory layout? Oh, of course not. But let's take a moment out of this issue and take a look at another important issue-memory alignment.

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 Microsoft C compiler (cl.exe for 80x86) in win32platform uses the following alignment rules by default: The alignment modulus of any basic data type T is the size of T, that is, sizeof (t ). For example, for the double type 8 bytes), the address of the Data Type must always be a multiple of 8, while the char type data (1 byte) can start from any address.

In Linux, GCC adopts another set of rules (not verified in the data, please correct the error): Any 2-byte size (including single-byte ?) The alignment modulus of data types (such as short) is 2, while all other data types (such as long and double) that exceed 2 bytes are 4 as alignment modulus.

Return to the struct we care about. Ansi c specifies that the size of a structure type is the sum of the size of all its fields and the size of the padding areas between or at the end of the field. Hmm? Fill area? Yes, this is the space allocated to the struct to make the struct field meet the memory alignment requirements. So what are the alignment requirements of the struct itself? Yes, the ansi c standard specifies that the alignment requirement of the struct type cannot be looser than the strictest one in all its fields (but this is not mandatory, vc7.1 is just as strict as they are ).

In actual development, we can change the alignment rules of the compiler by specifying/ZP compilation options. For example, specifying/zpn (N in vc7.1 can be 1, 2, 4, 8, or 16) tells the compiler that the maximum alignment modulus is N. In this case, the alignment rules of all basic data types smaller than or equal to n Bytes are the same as those of the default one, but the alignment modulus of Data Types greater than n Bytes is limited to n. In fact, the default alignment option of vc7.1 is equivalent to/zp8. By taking a closer look at msdn's description of this option, we will find that it solemnly warns programmers not to use the/zp1 and/zp2 options on MIPS and Alpha platforms, do not specify/zp4 and/zp8 on a 16-bit platform (think about why ?).
######################################## ##########################

References:
Memory alignment problem-http://blog.ednchina.com/jasony/92132/message.aspx
Also talk about memory alignment-http://bigwhite.blogbus.com/logs/1347304.html

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.