为什么我的结构元素进行填充字节? [英] Why does my structure element carry padding bytes?
问题描述
我有一个结构元素的内存对齐一个混乱的行为。考虑这两种结构:
typedef结构s_inner {
无符号长UL1;
双DBL1;
fourth_struct S4;
无符号长UL2;
INT I1;
} t_inner;
typedef结构s_outer {
other_struct member1中; / * 4字节对齐,40的sizeof()而言字节* /
无符号长member2上;
t_inner member3; /* 往上看 */
} t_outer;
当我检查t_outer的内存布局,我可以看到 member1中
的元素是4字节对齐的,正如我所期望的。
此外 member3
的内存布局是按预期: UL1
有附加4填充字节,这样 DBL1
对齐的8字节边界(正常在Win32)。
但是,当我检查 member2上
的内存布局,我可以看到,这件具有连接到它4填充字节。
任何人都可以解释为什么地球上的 member2上
接收填充字节?我的期望是, member2上
不进行填充。
修改1:
看到这个内存转储。填充结构元素之前,我有 memset的
倒是全 t_outer
结构点
的:
- 的红色区域是
member1中
- 蓝色区域是
member2上
- 绿化面积
member3
- 在黄色区域内
member3
标记的DBL1
位置
约束的
- 在编译器是VS2012
- 的实际stucture
other_struct
不应该事在这里,这是一个40字节大小的4字节对齐的结构 - 在我做的不可以想要的任何变通办法的行为(重新排序,包装,...),但解释为什么会这样。
,使得DBL1上的8字节边界对齐的
当然。但该路线的保证意味着bupkis如果结构本身没有对齐8为好。这是一个保证,是的正常的用于数据段和堆栈帧提供的编译器的地址选择。或者内存分配器。所有保证至少调整到8。
但是,当你里面嵌入结构的 s_outer 的那么这些4个字节填充的前的member3(不member2上之后)都必须获得对准保证了。
另外请注意,一个结构的最后一个成员后,有填充。可能需要以确保当所述结构被存储在数组成员仍然对准。同样的道理。
I have a confusing behaviour with the memory alignment of structure elements. Consider these two structures:
typedef struct s_inner {
unsigned long ul1;
double dbl1;
fourth_struct s4;
unsigned long ul2;
int i1;
} t_inner;
typedef struct s_outer {
other_struct member1; /* 4-byte aligned, 40 bytes in terms of sizeof() */
unsigned long member2;
t_inner member3; /* see above */
} t_outer;
When I inspect the memory layout of t_outer, I could see that the elements of member1
are 4-byte aligned, just as I would expect it.
Also the memory layout of member3
is as expected: ul1
has 4 padding bytes attached so that dbl1
is aligned on an 8-byte border (normal on Win32).
However, when I inspect the memory layout of member2
, I could see that this member has 4 padding bytes attached to it.
Could anyone explain why on earth member2
receives padding bytes? My expectation was that member2
does not carry a padding.
Edit 1:
See this memory dump. Before filling the structure elements, I've memset
'd the whole t_outer
structure with p
's:
- the red area is
member1
- the blue area is
member2
- the green area is
member3
- the yellow area marks the location of
dbl1
withinmember3
Constraints
- Compiler is VS2012
- the actual stucture of
other_struct
should not matter here, it's a 40-byte sized 4-byte aligned structure - I do not want any workarounds for the behavior (reordering, packing, ...) but an explanation why this is happening.
so that dbl1 is aligned on an 8-byte border
Sure. But that alignment guarantee means bupkis if the structure itself is not aligned to 8 as well. Which is a guarantee that's normally provided by the compiler's address choices for the data section and the stack frame. Or the memory allocator. All guarantee at least alignment to 8.
But when you embed the structure inside s_outer then those 4 bytes of padding before member3 (not after member2) are required to get the alignment guarantee back.
Also note that a structure can have padding after the last member. Which may be required to ensure that members are still aligned when the structure is stored in an array. Same reason.
这篇关于为什么我的结构元素进行填充字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!