I. effect and reason of byte alignment:
Alignment functions and causes: the processing of storage space varies greatly by hardware platform. Some platforms can only access certain types of data from some specific addresses. For example, some architectures may encounter errors when the CPU accesses a variable that is not aligned, so in this architecture, the programming must ensure the byte alignment, and other platforms may not, however, if the data storage is not aligned according to the appropriate platform requirements, the access efficiency will be compromised. For example, some platforms start from the even address each time they read data. If an int type (assuming a 32-bit System) is stored at the beginning of the even address, the 32bit can be read in a read cycle, if the data is stored at the beginning of the odd address, two read cycles are required, and the high and low bytes of the two read results are pieced together to obtain the 32bit data, obviously, reading efficiency is greatly reduced.
Ii. byte alignment rules:
Four important concepts:
1.Alignment of the Data Type itself: For char data, its own alignment value is 1, for short type is 2, for int, float, double type, its own alignment value is 4 bytes.
2.Alignment of struct or Class: The value with the largest alignment value in the member.
3.Specify alignment Value: # Alignment value specified when Pragma pack (value.
4.Valid alignment values of data members, struct, and classes: The alignment value and the value smaller than the specified alignment value.
Supplement:
1). Each member is aligned in its own way, and the length can be minimized.
2). The default alignment of complex types (such as structures) is the alignment of its longest member, so that the length can be minimized when the member is a complex type.
3) The alignment length must be an integer multiple of the largest alignment parameter in the member, so that each item can be aligned at the boundary when processing the array.# Pragma pack (1) <br/> struct test <br/> {<br/> static int A; // Static Var <br/> double M4; <br/> char M1; <br/> int m3; <br/>}< br/> # pragma pack () <br/> // sizeof (test) = 13; </P> <p> class test1 {}; <br/> // sizeof (test1) = 1; </P> <p>/* indicates: <br/> 1. Static members in a structure or class do not affect the size of the structure or class because the storage location of static variables is irrelevant to the instance address of the structure or class; <br/> 2. No member variable structure or class size is 1, because each instance of the structure or class must have a unique address in the memory. */<Br/>
Example:
// Analyze the following example C: <br/> // <br/> # pragma pack (2) /* specify 2-byte alignment */<br/> struct C <br/> {<br/> char B; <br/> int; <br/> short C; <br/>}; <br/> # pragma pack () // restore the alignment state <br/>/* <br/> the alignment value of the first variable B is 1 and the alignment value is 2. Therefore, the valid alignment value is 1. Assume that C starts from 0x0000, then B is stored in 0x0000, which complies with 0 x 0000% 1 = 0; <br/> the second variable, the alignment value is 4 and the specified alignment value is 2. Therefore, the valid alignment value is 2, therefore, the data is stored in four consecutive bytes, namely 0x0002, 0x0003, 0x0004, and 0 x 0002%. <Br/> the alignment value of the third variable C is 2, so the valid alignment value is 2. The order is 0x0006, 0 x 0006% 2 = 0. <Br/> therefore, the variable struct C is stored in a total of eight characters from 0x0000 to 0x0007. And struct C's own alignment value is 4, so the valid alignment value of struct C is 2. <Br/> again 8% 2 = 0, struct C only occupies eight bytes from 0x0000 to 0x0007. So sizeof (struct c) = 8. <Br/> if you change # pragma pack (2) to # pragma pack (4), the size of the structure is 12. <Br/> */
// Let's look at the example below <br/> // <br/> # pragma pack (8) <br/> struct S1 <br/>{< br/> char; <br/> long B; <br/>}; <br/> struct S2 {<br/> char C; <br/> struct S1 D; <br/> long e; <br/>}; <br/> # pragma pack () </P> <p> sizeof (S2) results in 24. <br/>/* <br/> S1: <br/> member A is 1 byte, which is aligned by 1 byte by default. The alignment parameter is 8, in the two values, 1 and a are aligned by 1 byte. <br/> member B is 4 bytes. By default, it is aligned by 4 bytes, therefore, sizeof (S1) should be 8; </P> <p> S2: <br/> C is the same as a in S1 and is aligned by 1 byte, <br/> D is a structure with 8 bytes. What is its alignment? For the structure, its default alignment is the largest of all its members using alignment parameters, and S1 is 4. therefore, member D is aligned in 4 bytes. <br/> member e is 8 bytes, which is aligned by 8 bytes by default. It is the same as the specified one, so it is directed to the boundary of 8 bytes, 12 bytes are used, SO 4 bytes are added with null values. The length of member e is 24, starting from 16th bytes. <br/>, it can be divisible by 8 (member e is aligned by 8 bytes. A total of 24 bytes are used. <br/> A B <br/> S1 memory layout: 11 **, 1111, <br/> C s1.a s1. B d <br/> S2 memory layout: 1 ***, 11 ***, 1111, *** 11111111 <br/> */
Iii. How should we consider byte alignment in programming?
If we want to save space during programming, we only need to assume that the first address of the structure is 0, and then sort the variables according to the above principles, the basic principle is to declare the variables in the structure according to the type size from small to large, and minimize the space to fill. Another way is to take the space for the efficiency of time, we show to fill the space for alignment, for example, there is a way to use the space for time is to explicitly insert reserved members:
Struct
{
Char;
Char reserved [3]; // use space for time
Int B;
};
The reserved member has no significance for our program. It just fills the space to achieve byte alignment. Of course, even if this member is not added, the compiler will automatically fill the alignment for us, we add it as a reminder.
Iv. potential risks of byte alignment
Many of the potential alignment risks in the Code are implicit. For example, in forced type conversion. For example:
Unsigned int I = 0x12345678;
Unsigned char * P = NULL;
Unsigned short * P1 = NULL;
P = & I;
* P = 0x00;
P1 = (unsigned short *) (p + 1 );
* P1 = 0x0000;
The last two lines of code access the unsigned short variable from the odd boundary, which obviously does not comply with the alignment rules. On x86, similar operations only affect the efficiency, but on MIPS or iSCSI, they may be an error because they require the bytes to be aligned.
5. How to find problems with byte alignment
If alignment or assignment occurs, first check
1. Alignment value set by the compiler
2. Check whether the system supports non-alignment access.
3. If alignment or alignment is supported, some special modifications are required to mark special access operations.