C language learning summary _09_ memory alignment of structure

1. Why is there memory alignment?
There is code below

#include<stdio.h>
#include<windows.h>
struct s1
{
    
    
	char c;
	int i;
	char c1;
};
struct s2
{
    
    
	char c;
	char c1;
	int i;
};
int main()
{
    
    
	printf("%d\n", sizeof(struct s1));
	printf("%d\n", sizeof(struct s2));

	system("pause");
	return 0;
}

Insert picture description here
The element types of the member variables in the two structures are the same, but the sizes of the two structures are different, which is very strange.
In fact, when the CPU accesses the memory address of the structure, there are certain rules, which must be obeyed ----> the memory alignment of the structure
So why is there memory alignment?
Most books and reference materials say this:
1. Platform Reason 9 (Reason for transplantation): Not all hardware platforms can access any data at any address; some hardware platforms can only fetch at certain addresses Some specific types of data, otherwise a hardware exception will be thrown.
2. Performance reasons:
data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to do two memory accesses, while aligned memory only needs one access.
In general: the memory alignment of the structure is the practice of trading space for time. (Because of the principle of memory alignment within the structure for memory access, the two member variables are very likely to have memory hollows, just like the different arrangements of the same member variables of the two structures above lead to different sizes of the structure)
2. Structure memory alignment rules:
1. The first member is at the address with an offset of 0 from the structure variable.
2. Other member variables must be aligned to an address that is an integer multiple of a certain number (alignment number). That is to say, the starting offset can divide the alignment number evenly.
The alignment number is equal to the smaller value of the compiler's default alignment number (you can set it yourself) and the size of the member variable, ==>min (default alignment number, member size).
3. The total size of the structure is an integer multiple of the maximum alignment number (each member variable has an alignment number).
Among them, the custom type, such as the maximum alignment of the array is
=>min (the size of one of the elements, the compiler's default alignment number), the largest alignment number of the structure itself is the largest one among the alignment numbers of its member variables.
Come on the code, calculate it.

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)

//结构体的内存对齐
typedef struct s1
{
    
    
	char c1;
	int i;
	char c2;
} s1_;
typedef struct s2
{
    
    
	char c1;
	char c2;
	int i;
} s2_;
typedef struct s3
{
    
    
	double c1;
	char c2;
	int i;
} s3_;
//结构体嵌套
struct s4
{
    
    
	char c1;
	struct s3 _s3;
	double d;
};
union Un2
{
    
    
	short c[7];
	int i;
};
//可以利用联合判断当前计算机的大小端存储
union Un1
{
    
    
	char c;
	int i;
};

int main()
{
    
    
	union Un1 un;
	un.i = 1;
	int ret = un.c;
	if (1 == ret){
    
    
		printf("小端机器!\n");
	}
	else{
    
    
		printf("大端机器!\n");
	}
	printf("%d字节\n", sizeof(union Un2));
	printf("%d字节\n", sizeof(s1_));
	printf("%d字节\n", sizeof(s2_));
	printf("%d字节\n", sizeof(s3_));
	printf("%d字节\n", sizeof(struct s4));

	
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/CZHLNN/article/details/110006248