Memory alignment in c language

Speaking of the length of the structure

When studying the structure, the teacher left a very interesting question:

#include<stio.h>
#include<string>
struct Node
{
    
    
	char name;
	int string;
	char ssex;
	
};
int main()
{
    
    
	int size;
	size = sizeof(Node);
	printf("%d   ", size);
	system("pause");
	return 0;
}

Obviously, we are looking for the number of bytes occupied by a structure; if we do not touch the concept of memory alignment, there is a high probability that an answer will be blurted out: 6. Because char occupies one byte, int occupies 4 bytes, and 1+4+1=6, the byte size of the structure is 6. Of course, this is wrong. When we turn on the compiler to compile and run, we find that the result is: 12 . And we just change the order in the structure,

#include<stio.h>
#include<string>
struct Node
{
    
    
	char name;
	char ssex;
	int string;

};
int main()
{
    
    
	int size;
	size = sizeof(Node);
	printf("%d   ", size);
	system("pause");
	return 0;

At this time, when the compiler compiles and runs, we will find that the result of the operation is: 8 ; How the result came out is worth discussing.

Memory alignment rules

1) The first address of the structure variable is an integer multiple of its longest basic type member

2) The offset of each member of the structure relative to the first address of the structure (offset) is an integer multiple of the member size

3) The total size of the structure is an integer multiple of the size of the widest basic type member of the structure

The specific rules are taken from Baidu Encyclopedia. Let's explain directly according to the above rules.
For the first program above, since the first data type of the structure is char, there is no doubt that according to rule 1, it occupies 1 byte, so the number of bytes of the structure at this time is 1. When it comes to the int type, since int occupies 4 bytes, according to rule 2, it must start from the offset so that its length is a multiple of 4, so 3 offsets are added, starting from 4, add Above the 4 bytes occupied by int, the length of the structure comes to 8. And to the last char, the number of bytes occupied is 1, but according to rule 3, the total size must be an integer multiple of the widest type, so when starting from 8, there is no doubt that you need to add 1 and 3 offsets Bit, make the total length to 12 to ensure that all three rules are met. So the final result came to 12.

For the second program, since the first data type of the structure is char, there is no doubt that according to rule 1, it occupies 1 byte, so at this time the number of bytes of the structure is 1, and the second It is also a char. Just continue to add 1. The total length comes to 2, and the third int type is reached. Because int occupies 4 bytes, according to rule 2, it must be offset to make its length 4 The multiple starts, so 2 offsets are added, and the first address comes to 4. Starting from 4, plus the 4 bytes occupied by int, the length of the structure comes to 8.
Let's look at another example:

#include<stdio.h>
#include<string>
struct Date
{
    
    
	char dada;
	int name;
	int score;  
};
struct node
{
    
    
	char name;
	char sddd;
	char ssex;
	int daef; 
	Date vie; 
	double dae;  
};
int main()
{
    
    
	int size;
	size = sizeof(node);
	printf("%d   ", size);
	system("pause");
	return 0;
}

We still use the above rules, starting from the main function from top to bottom, the first is 3 char types, the number of bytes comes to 3. According to the above rules, the following int is offset by an offset, starting from 4, plus 4 bytes of itself, and the total length is 8. Then I encountered a structure variable, and turned my eyes to the previous structure Date. First, it was char, the length came to 1, and it was int. First, the offset 3 came to 4, plus 4 bytes, the length came. 8. Finally, add an int to 12, so the total length of Date is 12, and the widest is 4. Therefore, the number of bytes in node starts from 8, plus 12, the total length is 20. Since the last one is a double, the length is 8, so the total length needs to be offset by 4, to 24, plus 8. So the total number of bytes of node is: 32.

Guess you like

Origin blog.csdn.net/ALITAAAA/article/details/103043116