①内存对齐
- 第一个属性开始 从0开始偏移
- 第二个属性开始 要放在 该类型的大小 与 对齐模数比 取小的值 的整数倍
- 所有属性都计算完后,再整体做二次偏移,将整体计算的结果 要放在 结构体最大类型 与对齐模数比 取小的值的 整数倍上
- 结构体嵌套结构体时候,子结构体放在该结构体中最大类型 和对齐模数比 的整数倍上即
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
typedef struct _test
{
char a;//0
char b;//1
int c;//4~8
}mytest01;
typedef struct _test02
{
char a;//0~3
int b;//4~8
char c;//9~11
}mytest02;
typedef struct _test03
{
char a;//0~3
mytest01 b;//4~11
int c;//12~15
float d;//16~19
}mytest03;
int main(void)
{
mytest01 a;
a.a = '1';
a.b = '2';
a.c = 12;
printf("sizeof=%d\n", sizeof(mytest03));
system("pause");
return EXIT_SUCCESS;
}
②修改内存对齐方式
#pragma pack(n)
n为对齐字节