#pragma pack (4)
int16u *d[d_number];
#pragma pack ()
#pragma pack (n)
Explanation One:
Compilers on each particular platform have their own default "alignment factor" (also known as the number of Zimo). Programmers can change this factor by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify.
Rules:
1. Data member Alignment rules: data members of a struct (struct) (or union), where the first data member is placed at offset 0, and subsequent alignment of each data member according to the value specified by the #pragma pack and the length of the data member itself, than the smaller one.
2, structure (or union) of the overall alignment rules: After the data members have completed their respective alignment, the structure (or union) itself will be aligned, alignment will follow the value specified by the #pragma pack and the structure (or union) of the maximum data member length, the smaller one.
EXPLANATION Two:
N-byte alignment VC special handling of the storage of the structure does increase the speed of the CPU storage variables, but sometimes it also brings some trouble, we also block out the default alignment of variables, we can set the alignment of variables. The #pragma pack (n) is provided in the VC to set the variable to n-byte alignment. N-byte alignment means that the offset of the starting address at which the variable is stored is in two cases:
First, if n is greater than or equal to the number of bytes consumed by the variable, the offset must satisfy the default alignment.
Second, if n is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n and does not satisfy the default alignment. The total size of the structure also has a constraint, in 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 number of spaces occupied by the variable occupying the largest space, otherwise it must be a multiple of n.
The following examples illustrate their usage. #pragma pack (push)//Save alignment status
#pragma pack (4)//set to 4-byte alignment
struct Test {char M1; double M4; int m3;}; #pragma pack (POP)//restore alignment state the size of the struct above is 16:
The following analysis of its storage, first allocated space for M1, its offset is 0, to meet our own set of alignment (4-byte alignment), m1 size is 1 bytes. It then begins allocating space for the M4, where the offset is 1, which requires 3 bytes to be filled so that the offset satisfies a multiple of n=4 (because sizeof (double) is greater than 4) and M4 takes up 8 bytes. The M3 is then allocated space, at which point the offset is 12, satisfies the multiple of 4, and M3 occupies 4 bytes. At this point, all member variables have been allocated space, with a total of 16 bytes allocated, which satisfies a multiple of n. If you change the #pragma pack (4) above to #pragma pack (8), then we can get the size of the structure to 24.
Everyone read these words to describe the head will also be numb it, I insisted on reading, and then wrote a program:
#pragma pack (4)
struct node{
int e;
Char F;
short int A;
Char b;
};
struct node n;
printf ("%d\n", sizeof (n));
The result of my own calculation is 16, and the actual result is:
Then the structure internal data member changes position:
#pragma pack (4)
struct node{
Char F;
int e;
short int A;
char b;};
struct node n;
printf ("%d\n", sizeof (n));
Force the alignment number to position 2
#pragma pack (2)
struct node{
Char F;
int e;
short int A;
char b;};
struct node n;
printf ("%d\n", sizeof (n));
Force the alignment number to position 1
#pragma pack (1)
struct node{
Char F;
int e;
short int A;
char b;};
struct node n;
printf ("%d\n", sizeof (n));
Look at the output and text description a little dizzy, the following is a brief statement of my decision rules:
In fact, the reason for the memory byte alignment mechanism is to minimize memory read times. We know that the CPU reads faster than the memory read speed at least one order of magnitude, so to save the computation time, can only sacrifice space to exchange time.
The following example shows how to minimize the number of reads.
#pragma pack (1)
struct node{
Char F; 1
int e; 4
short int A; 2
Char b; 1
};
struct node n;
printf ("%d\n", sizeof (n));
Here the Force is aligned in accordance with 1 bytes , it can be understood that all the content is read according to 1 bytes (for the moment, because this is a good understanding of the memory mechanism), all the other data members are 1-byte integer multiples, so there is no memory to it, Each member is arranged in memory in the actual order, the actual length of the struct is 8
#pragma pack (2)
struct node{
Char F;
int e;
short int A;
char b;};
struct node n;
printf ("%d\n", sizeof (n));
This forces the alignment by 2 bytes . If the memory distribution is still continuous, then int e will have three times to read the CPU, so in order to "pay attention to" int e reading, so after char f reserved 1BYTE, the last Char B is the same, so the length is 10
#pragma pack (4)
struct node{
Char F;
int e;
short int A;
char b;};
struct node n;
printf ("%d\n", sizeof (n));
This forces the alignment by 4 bytes. So char f is reserved for 3BYTE, and short int A and char B can be read to the CPU at one time(read by 4 bytes), so the length is 12
If n in #pramga pack (n) is greater than the number of bytes occupied by any member of the struct member, the n value is invalid. The compiler chooses the number of bytes of the largest data member in the struct as the basis for its
Common C-language functions-alignment