结构的大小是否需要是该结构对齐的精确倍数? [英] Is the size of a struct required to be an exact multiple of the alignment of that struct?
问题描述
我再次质疑一个长期存在的信念.
Once again, I'm questioning a longstanding belief.
直到今天,我认为下面结构体的对齐通常是4,大小通常是5...
Until today, I believed that the alignment of the following struct would normally be 4 and the size would normally be 5...
struct example
{
int m_Assume_32_Bits;
char m_Assume_8_Bit_Bytes;
};
由于这个假设,我有使用 offsetof 来确定数组中两个相邻项之间的字节距离的数据结构代码.今天,我发现一些不应该使用 sizeof 的旧代码,不明白为什么我没有从中发现错误,编写了一个单元测试 - 测试通过了让我感到惊讶.
Because of this assumption, I have data structure code that uses offsetof to determine the distance in bytes between two adjacent items in an array. Today, I spotted some old code that was using sizeof where it shouldn't, couldn't understand why I hadn't had bugs from it, coded up a unit test - and the test surprised me by passing.
一些调查表明,我用于测试的类型的大小(类似于上面的结构)是对齐的精确倍数 - 即 8 个字节.它在最后一个成员之后有填充.这是一个为什么我没想到会发生的例子......
A bit of investigation showed that the sizeof the type I used for the test (similar to the struct above) was an exact multiple of the alignment - ie 8 bytes. It had padding after the final member. Here is an example of why I never expected this...
struct example2
{
example m_Example;
char m_Why_Cant_This_Be_At_Offset_6_Bytes;
};
一些谷歌搜索显示的示例清楚地表明允许在最终成员之后进行填充 - 例如 http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding(或在结构末尾"位).
A bit of Googling showed examples that make it clear that this padding after the final member is allowed - for example http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding (the "or at the end of the structure" bit).
这有点尴尬,因为我最近发布了这个评论 - 使用结构填充(我对该答案的第一条评论).
This is a bit embarrassing, as I recently posted this comment - Use of struct padding (my first comment to that answer).
我似乎无法确定的是,C++ 标准是否保证这种填充到对齐的精确倍数,或者它是否只是允许的并且某些(但可能不是全部)编译器这样做.
What I can't seem to determine is whether this padding to an exact multiple of the alignment is guaranteed by the C++ standard, or whether it is just something that is permitted and that some (but maybe not all) compilers do.
那么 - 根据 C++ 标准,结构的大小是否需要是该结构对齐的精确倍数?
So - is the size of a struct required to be an exact multiple of the alignment of that struct according to the C++ standard?
如果 C 标准做出不同的保证,我也对此感兴趣,但重点是 C++.
If the C standard makes different guarantees, I'm interested in that too, but the focus is on C++.
推荐答案
alignment 大小的一个定义:
结构的 alignment 大小是当您拥有该结构的数组时从一个元素到下一个元素的偏移量.
The
alignmentsize of a struct is the offset from one element to the next element when you have an array of that struct.
就其性质而言,如果您有一个包含两个元素的结构数组,那么两者都需要有对齐的成员,这意味着是的,大小必须是对齐的倍数.(我不确定是否有任何标准明确强制执行这一点,但是因为结构的大小和对齐方式不取决于结构是单独的还是在数组内,所以相同的规则适用于两者,所以它不能真的以任何其他方式.)
By its nature, if you have an array of a struct with two elements, then both need to have aligned members, so that means that yes, the size has to be a multiple of the alignment. (I'm not sure if any standard explicitly enforce this, but because the size and alignment of a struct don't depend on whether the struct is alone or inside an array, the same rules apply to both, so it can't really be any other way.)
这篇关于结构的大小是否需要是该结构对齐的精确倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!