基本要求
struct对其要求:
- may need to insert gaps in the field allocation
由于结构体中变量各自的对齐要求,所以内存的分配中间会留空 - may need to add padding to the end of the structure
strcut的总长度,要求是其含有最大数据类型长度的整数倍
因此末尾会留下填充的空白
例子
-
Gap:
-
Padding:
如果成员是struct呢?
struct InnerStruct {
char c;
int i;
long l;
};
struct OuterStruct {
char a;
struct InnerStruct inner; //InnerStruct作为OuterStruct的成员
short s;
};
这种情况下,OuterStruct的对其要求是8对齐,因为InnerStruct是8对齐
(因为InnerStruct中最大类型是long)
更多文章:
计算机编译程序的原理
函数调用的汇编原理
汇编语句详解(持续更新)
关于位运算必须记住的事
C语言中的类型转换