是一个结构的大小要求是该结构的调整的整数倍? [英] Is the size of a struct required to be an exact multiple of the alignment of that struct?

查看:120
本文介绍了是一个结构的大小要求是该结构的调整的整数倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再一次,我质问一个长期的信念。

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,来确定数组中的两个相邻项目之间以字节为单位的距离数据结构code。今天,我发现它不应该,不明白为什么我没有过错误的话,codeD了单元测试的一些旧的code,它是用的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.

调查有点表明,我用于测试(类似于上面的结构)sizeof的类型是对齐的整数倍 - 即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;
};

谷歌搜索的结果显示一点例子,说清楚,最后的成员后,该填充是允许的 - 例如:<一href="http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding">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++.

推荐答案

的一个定义<打击>调整大小:

的<击>调整的结构体的大小是由一个元素偏移到下一个元素,当你有一个结构的数组。

The alignment size 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆