Memory alignment allocation policy (including bit domain mode), alignment

Source: Internet
Author: User
Tags modulus microsoft c

Memory alignment allocation policy (including bit domain mode), alignment

1: Memory alignment definition:
The memory space in the current computer is divided by byte. Theoretically, it seems that access to any type of variables can start from any address, however, in fact, computer systems have limits on the storage location of basic data types in the memory, requiring that the first address of the data storage be a multiple of a certain number of K, in this way, the various basic data types are arranged according to certain rules in the memory, rather than one next to the other, which is the memory alignment.

Alignment modulus:
The value K specified in the memory Alignment is Alignment Modulus ). 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 ).

2: benefits of memory alignment:
Memory alignment is a mandatory requirement. First, it simplifies the design of the transmission system between the processor and the memory, and second, it can speed up Data Reading. The processing of buckets 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, programming must ensure byte alignment. this may not be the case for other platforms, but the most common problem is that alignment of data storage according to the requirements of their platforms may cause a loss of access efficiency. 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.
Intel's IA32 architecture processor works correctly regardless of whether the data is aligned. However, if you want to improve performance, pay attention to the memory alignment mode.
The ansi c standard does not specify 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 basic data types (int char, etc.), the memory space they occupy has a definite value in a specific hardware system. 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.

3: Memory alignment policy:
Alignment policy of Microsoft C compiler (cl.exe for 80 × 86:
First, 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, it first finds the widest basic data type in the struct, and then finds the location where the memory address can be divisible by the basic data type, the first address of the struct. Use the size of the widest basic data type as the alignment modulus described above.
Second, 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 pre-opening space to the first address of the struct is an integer multiple of the current member. If yes, It stores the member, on the contrary, a certain number of bytes are filled between the current member and the previous Member to meet the integer double requirement, that is, the first address of the pre-opened space is removed several bytes.
Third: 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.

The padding byte is the space allocated to the struct to make the struct field meet the memory alignment requirements. The structure itself also has alignment requirements. The ansi c standard specifies that the alignment requirements of the structure type cannot be looser than the strictest requirements of all its fields, but it can be more strict (but this is not mandatory, VC7.1 just makes them as strict ). The C standard ensures that the space occupied by arrays of any type (including custom structure types) must be equal to the size of a single data of this type multiplied by the number of array elements. In other words, there is no gap between the elements of the array.

The summary rules are as follows:
0: the first address of the struct variable can be divisible by the size of its widest basic type member.
1: The default memory alignment of VC6 and VC71 is # pragam pack (8)
2: each member in the struct is aligned according to its type (usually the size of this type) and a smaller alignment in the specified alignment parameter.
3: The offset of each member of the struct to the first address of the struct is an integer multiple of the member size.
4: The structure itself also has an alignment requirement rule, which cannot be looser than the strictest requirement of all its fields.
5: the total size of the struct is an integer multiple of the size of the widest basic type of the struct, and the memory should be saved as much as possible.
6: in GCC, the maximum alignment modulus is 4. That is to say, the alignment modulus can only be 1 or 2 even if there is a double type in the structure, 4.
In addition, in the above rules, the offset value must be an integer multiple of the member size in the first 3rd:
(1): If the member size is smaller than or equal to 4, it is feasible according to the above rules,
(2): If the size of a member 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.

typedef struct ms1 {  char a;  int b;} MS1;typedef struct ms2 {  int a;  char b;} MS2; 

The strongest alignment requirement in MS1 is the B field (int type). The first address offset of field a is 0 (a multiple of 1), which is directly stored. If field B is directly stored, the offset of field B relative to the first address of the struct variable is 1 (not a multiple of 4), which is filled with 3 bytes, and B is stored starting from the offset address of 4. That is to say, 2nd and 3rd rules are followed. For the struct variable itself, the alignment parameter should be at least 4 according to Rule 4. According to Rule 5, sizeof (MS1) = 8; the same is true for the result obtained by MS2 analysis.

typedef struct ms3 {  char a;  short b;  double c;} MS3;typedef struct ms4 {  char a;  MS3 b;} MS4; 

In MS3, the most strict memory field is c (8 bytes), and the alignment parameter of MS3 is also 8 bytes; the alignment modulus of the MS4 data type is the same as that of the double type (8) in MS3. Field a should be followed by 7 bytes. sizeof (MS3) = 16; sizeof (MS4) = 24;
Note that in rule 5, the total size of the struct is an integer multiple of the size of the widest basic type of the struct. Note that it is the basic type. Here, MS3 is not the basic type.
The choice of alignment modulus can only be based on the basic data type. Therefore, for the nested struct In the struct, you can only consider the basic data type to be split.

Example 3 (GCC): struct T {char ch; double d ;};

In GCC, sizeof (T) should be 12 bytes. 16 bytes in VC8.
Ch is 1 byte. No problem. The size of the subsequent d is greater than 4. The alignment modulus of d can only be 4, and the first address offset relative to the struct variable can only be 4, instead, it cannot be an integer multiple of 8, which is stored at the offset of 4. The struct occupies 12 bytes in total.
No 5th rules are executed here.

Bit domain status:
C99 specifies that int, unsigned int, and bool can be bit domain types. However, almost all compilers have extended this to allow the existence of other types.
If the struct contains a bit-field, the summary rules are as follows:
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;
4) do not compress fields that are interspersed with non-bit fields;
5) the total size of the struct is an integer multiple of the size of the widest basic type of the struct, and the memory should be saved as much as possible.
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.

Example 4: typedef struct {char c: 2; double I; int c2: 4;} N3;

According to rule 4, the space occupied by GCC is 16 bytes, and the space occupied by VC is 24 bytes. Conclusion:
--------
When defining a struct, it is best for Members to define it from large to small, which can save space relatively. For example:

struct A {  double d;  int i;  char c;}; 

Therefore, both vc series compilers in windows and gcc in linux are 16 bytes.

Example 5: typedef union student {char name [10]; long sno; char sex; float score [4];} STU; STU aa [5]; cout <sizeof (aa) <endl;

Union is variable. The maximum Member of the union is 16*5 = 5 = 80.

Example 6: typedef struct student {char name [10]; long sno; char sex; float score [4];} STU; STU aa [5]; cout <sizeof (aa) <endl;

Space occupied by STU: 10 bytes (char) + null 2 bytes + 4 bytes (long) + 1 byte (char) + null 3 bytes + 16 bytes (float) = 36 bytes, 36*5 = 180 bytes

Example 7 (VC8.0): typedef struct bitstruct {int b1: 5; int b2: 2; int b3: 3;} bitstruct; int _ tmain (int argc, _ TCHAR * argv []) {bitstruct B; memcpy (& B, "EM", sizeof (B); cout <sizeof (B) <endl; cout <B. b1 <endl <B. b2 <endl <B. b3; return 0 ;}

Bitstruct is a struct containing a bit field. The sizeof (int) is 4 bytes. According to rules 1 and 2, b1 occupies the first 5 bytes. According to rule 1, b2. B2. B3. B3.
According to Rule 5, obtain sizeof (bitstruct) = 4.
Currently, the mainstream CPU, intel series uses the little endian format to store data, and motorola series CPUs use big endian.
Using mainstream little endian analysis:
During memory allocation, the first member type int (4 bytes) of bitstruct is allocated first. The storage of these four bytes follows the principle that low bytes are stored in low addresses.
Int contains four bytes:
4th bytes-3rd bytes-2nd bytes-1st bytes-bytes,

The storage method in the memory is as follows.
Then allocate 5 bits to b1. Here, the priority should be 5 lower bits, that is, the 5 lower bits of the first byte.
Then the two bytes of b2 are allocated, that is, the second byte following the 1st bytes.
Finally, the three bits of b3 are allocated. According to rules 1, 2, and b3, the bits are stored immediately. The bits of b3 are the highest bits of the first byte, and the two bits are the lowest bits of the 2nd byte.
The memory distribution chart is as follows:

Typedef struct bitstruct {int b1: 5; int b2: 2; int b3: 4;} bitstruct; int _ tmain (int argc, _ TCHAR * argv []) {bitstruct B; memcpy (& B, "EM", sizeof (B); cout <sizeof (B) <endl; cout <B. b1 <endl <B. b2 <endl <B. b3; return 0 ;}

4: defines the memory layout and memory byte alignment for Arrays

Int B = 10;

Int a [3] = {1, 2, 3 };

Int c = 11;

Low address (stores the lowest byte)

 

1 void _ cdecl func_cdcel (int I, char * szTest) {2 3 cout <"szTest address IN STACK:" <& szTest <endl; 4 5 cout <"szTest value (pointing address):" <(void *) szTest <endl; 6 7 8 9 cout <"I in stack address:" <& I <endl; 10 11 cout <"I address: "<& I <endl; 12 13 14 15 int k, k2; 16 17 cout <" Address of the local variable k: "<& k <endl; 18 19 cout <"Address of local variable k2:" <& k2 <endl; 20 21 cout <"-------------------------------------------------------" <endl; 22 23} 24 25 26 27 void _ stdcall func_stdcall (int I, char * szTest) {28 29 cout <"szTest address in the stack: "<& szTest <endl; 30 31 cout <" szTest's own value (pointing to address): "<(void *) szTest <endl; 32 33 34 35 cout <"I in stack address:" <& I <endl; 36 37 cout <"I address: "<& I <endl; 38 39 40 41 int k, k2; 42 43 cout <" Address of the local variable k: "<& k <endl; 44 45 cout <"Address of local variable k2:" <& k2 <endl; 46 47 cout <"-------------------------------------------------------" <endl; 48 49} 50 51 52 53 int main () {54 55 int a [4]; 56 57 cout <"a [0] address: "<& a [0] <endl; 58 59 cout <" a [1] address: "<& a [1] <endl; 60 61 cout <"a [2] address:" <& a [2] <endl; 62 63 cout <"a [3] address: "<& a [3] <endl; 64 65 66 67 int I = 0x22; 68 69 int j = 8; 70 71 char szTest [4] = {'A', 'B', 'C', 'D'}; 72 73 cout <"I address: "<& I <endl; 74 75 cout <" szTest address: "<(void *) szTest <endl; 76 77 func_cdcel (I, szTest ); 78 79 func_stdcall (I, szTest); 80 81}View Code

Output:

A [0] address: 0012FF54
A [1] address: 0012FF58
A [2] address: 0012FF5C
A [3] address: 0012FF60 <-the visible storage method is shown in. a [3] is in the high address. First, go to the stack, array address a is a [0] address (low address)
I address: 0012FF48 <-memory alignment is performed here. The starting address of I must be a multiple of the memory size occupied by I.
SzTest address: 0012FF30

SzTest stack address: 0012FE5C
SzTest value (pointing address): 0012FF30

I in the stack address: 0012FE58 <-I in the stack address is lower than szTest, that is, szTest is first in the stack
Address of I: 0012FE58
Address of the local variable k: 0012FE48
Address of local variable k2: 0012FE3C
-------------------------------------------------------
SzTest stack address: 0012FE5C
SzTest value (pointing address): 0012FF30

I in the stack address: 0012FE58
Address of I: 0012FE58
Address of the local variable k: 0012FE48
Address of local variable k2: 0012FE3C

 

Reprinted: http://www.cnblogs.com/alex-tech/archive/2011/03/24/1993856.html

 

Related Article

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.