更新时间:2023-11-18 20:46:04
填充的工作方式完全取决于实现.
How padding works depends entirely on the implementation.
对于具有两个字节的short
和四个字节的int
且类型必须与其大小倍数对齐的实现,您将具有:
For implementations where you have a two-byte short
and four-byte int
and types have to be aligned to a multiple of their size, you will have:
Offset Var Size
------ ---- ----
0 c1 1
1 ?? 1
2 s1 2
4 c2 1
5 ?? 3
8 i1 4
12 next
无论出于何种原因,实现都可以***地在结构的字段之间插入填充,并在最后一个字段之后(但在第一个字段之前 not )插入填充.在结构后填充的能力对于对齐阵列中的后续元素很重要.例如:
An implementation is free to insert padding between fields of a structure and following the last field (but not before the first field) for any reason whatsoever. The ability to pad after a structure is important for aligning subsequent elements in an array. For example:
struct { int i1; char c1; };
可能会给您:
Offset Var Size
------ ---- ----
0 i1 4
4 c1 1
5 ?? 3
8 next
填充通常是因为对齐数据工作更快,或者对齐数据是非法的(某些CPU体系结构不允许对齐访问).
Padding is usually done because either aligned data works faster, or misaligned data is illegal (some CPU architectures disallow misaligned access).