About the memory size of the structure
We first need to understand a related concept ———— Offset . Offset refers to the difference between the address of the member in the structure variable and the address of the structure variable . The structure size is equal to the offset of the last member plus the size of the last member .
struct book {
int num1;
char c1;
}book1;
Suppose we define a structure book1, The first member variable num1, here num1 The offset of is 0( Because at the moment num1 The address is the first address of the memory space occupied by the structure , So the difference between the two is 0), The memory space used at the moment is (0+4)( Offset + The byte size of this variable ), The second member variable is c1, The offset is the sum of the offsets of the previous variables 4. And the passage at this time sizeof The output is not 5(4+1, Offset + The bytes of this variable are large ), Why ?
In practice, , Address alignment is required when storing variables , The compiler will follow two principles when compiling programs :
- The offset of a member in a struct variable must be an integral multiple of the size of the member (0 Considered to be an integral multiple of any number )
- The structure size must be an integral multiple of the size of all members , That is, the common multiple of the size of all members .
In the structure we just defined book1 in , The result we think is 5, But in this alignment rule ,5 Not at all int Multiple of bytes , So it will be in the variable c1 Fill in the back 3 Bytes of space .
The latter is supplemented 3 Bytes of space . So by keyword sizeof The output result is 8.
Different sorting methods of structure members , Resulting in different structures .
Consider the following code :
#include<stdio.h>
struct book {
int num1;
char c1;
char c2;
}book1;
int main(int argc,char *argv[]){
printf("%d",sizeof book1);
return 0;
}
Through the above offset concept and alignment rules, we get the result of 8. The first member variable num1 The offset of is 0, The second member variable c1 The offset of is 4, Third member c2 The offset of the variable is (4+1), Finally, the memory occupied is equal to c2 Offset plus c2 The byte size of is equal to 6(4+1+1), Then follow the alignment rules , The maximum number of bytes in the structure member variable is int type , So I'll add it later 2 Bytes to 8, Can be 4 to be divisible by .
#include<stdio.h>
struct book {
char c1;
int num1;
char c2;
}book1;
int main(int argc,char *argv[]){
printf("%d",sizeof book1);
return 0;
}
By changing the order of the first and second member variables , We found that through keywords sizeof The output value is 12. The first member variable c1 The offset of is 0, The second member variable num1 The offset size of is 1, Different from the above is the offset at the moment 1 No int Multiple of bytes occupied by type .
Therefore, we need to make up at this moment 3 Bytes . So the third member variable c2 The offset of is 8, The final size is (1+4+3+1)9,9 No 4 Multiple , I'll fill it up later 3 Byte size , Become int The multiple of byte size occupied by type variable .