直接上例子:
typedef struct xx
{
double a; //8字节
char b; //16
char c; //16
char d; //16
char e; //16
char f; //16
char g; //16
char h; //16
char i; //16
char j; //24
}XX;
typedef struct yy
{
double a; //8字节
short b; //16
short c; //16
short d; //16
short e; //16
short f; //24
short g; //24
short h; //24
short i; //24
short j; //32
}YY;
typedef struct zz
{
double a; //8字节
int b; //16
int c; //16
int d; //24
int e; //24
int f; //32
int g; //32
int h; //40
int i; //40
int j; //48
}ZZ;
typedef struct aa
{
char a; //1字节
short b; //4
int c; //8
char d; //12
int e; //16
short f; //20
float g; //24
double h; //32
char i; //40
char j; //40
char k; //40
char l; //40
char m; //40
short n; //40
int o; //48
}AA;
typedef struct bb
{
double a; //8字节 8
char b; //16 9
int c; //16 9+3+4=16
short d; //24 16+2=18
double e; //32 18+6+8=32
int f; //40 32+4=36
short g; //40 36+2=38
float h; //48 38+2+4=44
int i; //48 44+4=48
int j; //56 48+4=52
char k; //56 52+1=53
short l; //56 53+1+2=56 56%8=0
short m; //64 56+2=58 58%8==0?58:58+6
int o; //64 58+2+4=64
char n; //72 64+1=65 65%8==0?65:65+(8-(65%8))
}BB;
得出最后公式:结构体大小size = (((当前累加字节大小)% (当前字节大小)==0 )?(当前累加字节大小)):(((当前累加字节大小)%(当前最大字节大小)==0)?(当前累加字节大小):((当前累加字节大小)+((当前最大字节大小)-((当前累加字节大小)%(当前最大字节大小))))。
%:取余运算
==:逻辑判断
+ - :算术运算
=:赋值运算
(? : ):三目运算符逻辑判断
晕了吧,上面公式逻辑比较复杂,可能笔者有考虑不到的地方,望见谅;