为什么对齐是2的幂? [英] Why alignment is power of 2?

查看:201
本文介绍了为什么对齐是2的幂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cppreference 中有一个引用:

每个对象类型都具有称为对齐要求的属性,该属性 是一个整数值(类型为std :: size_t,始终为2的幂) 代表连续地址之间的字节数 可以分配这种类型的对象.

Every object type has the property called alignment requirement, which is an integer value (of type std::size_t, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated.

我了解,此引用是非规范性的.但是标准中关于alignof(T)的值并没有什么,而不是不超过alignof(std::max_align_t).

I understand, this reference is non-normative. But there is no something about value of alignof(T) in the standard, rather than it is no more than alignof(std::max_align_t).

对齐显然不是2的幂.为什么对齐不是3?

It is not obviously, that alignment is power of 2. Why does alignment not be a 3?

推荐答案

该标准为该语言的最终字词,因此此处引用了该部分的内容.我加粗了2的幂要求:

The standard has the final word for the language, so here a quote of that section. I bolded the power-of-2 requirement:

3.11对齐方式[basic.align]

1对象类型具有对齐要求(3.9.1,3.9.2),这对可分配该类型对象的地址施加了限制.对齐方式是实现定义的整数值,表示可以分配给定对象的连续地址之间的字节数.对象类型对该类型的每个对象都要求对齐要求;可以使用比对说明符(7.6.2)请求更严格的比对.
2基本对齐方式由小于或等于实现在所有上下文中支持的最大对齐方式的对齐方式表示,该对齐方式等于alignof(std :: max_align_t)(18.2).将类型用作完整对象的类型并将其用作子对象的类型时,类型所需的对齐方式可能会有所不同. [示例:

3.11 Alignment [basic.align]

1 Object types have alignment requirements (3.9.1, 3.9.2) which place restrictions on the addresses at which an object of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. An object type imposes an alignment requirement on every object of that type; stricter alignment can be requested using the alignment specifier (7.6.2).
2 A fundamental alignment is represented by an alignment less than or equal to the greatest alignment supported by the implementation in all contexts, which is equal to alignof(std::max_align_t) (18.2). The alignment required for a type might be different when it is used as the type of a complete object and when it is used as the type of a subobject. [ Example:

struct B { long double d; };
struct D : virtual B { char c; }

当D是完整对象的类型时,它将具有类型B的子对象,因此必须对齐 适当的长双.如果D出现为另一个也具有B作为虚拟基类的对象的子对象,则B子对象可能是另一个子对象的一部分,从而降低了D子对象的对齐要求. -最终示例] alignof运算符的结果反映了在完整对象情况下该类型的对齐要求.
3扩展的对齐方式由大于alignof(std :: max_align_t)的对齐方式表示.它是 实现定义了是否支持任何扩展的对齐方式以及它们所处的上下文 支持(7.6.2).具有扩展对齐要求的类型是过度对齐类型. [注意:每个过度对齐的类型都是或包含适用于扩展对齐的类类型(可能通过非静态数据成员). -尾注]
4对齐方式表示为std::size_t类型的值.有效的对齐方式仅包括由alignof表达式为基本类型返回的那些值,以及由实现定义的其他值集,这些值可能为空. 每个对齐值均应为2的非负整数幂.
5路线的顺序是从弱到强或从严格到严格.更严格的比对具有更大的比对值.满足对齐要求的地址也满足所有较弱的有效对齐要求.

When D is the type of a complete object, it will have a subobject of type B, so it must be aligned appropriately for a long double. If D appears as a subobject of another object that also has B as a virtual base class, the B subobject might be part of a different subobject, reducing the alignment requirements on the D subobject. —end example ] The result of the alignof operator reflects the alignment requirement of the type in the complete-object case.
3 An extended alignment is represented by an alignment greater than alignof(std::max_align_t). It is implementation-defined whether any extended alignments are supported and the contexts in which they are supported (7.6.2). A type having an extended alignment requirement is an over-aligned type. [ Note: every over-aligned type is or contains a class type to which extended alignment applies (possibly through a non-static data member). —end note ]
4 Alignments are represented as values of the type std::size_t. Valid alignments include only those values returned by an alignof expression for the fundamental types plus an additional implementation-defined set of values, which may be empty. Every alignment value shall be a non-negative integral power of two.
5 Alignments have an order from weaker to stronger or stricter alignments. Stricter alignments have larger alignment values. An address that satisfies an alignment requirement also satisfies any weaker valid alignment requirement.

为什么所有实现都符合该要求(这是它可以全部包含的部分原因)?

Why did all implementations conform to that requirement (That's part of the reason it could be included at all)?

好吧,因为自然会在 binary 中乘以/除以/掩盖2的幂,并且所有系统都是(不包括某些真正古老的系统),并且在可预见的将来将保持基本是二进制.
自然,意味着它比任何其他乘法/除法/模运算都要高效得多,有时要高几个数量级.

Well, because it is natural to multiply / divide / mask powers of 2 in binary, and all systems were (excluding some really ancient ones), are, and for the foreseeable future will stay fundamentally binary.
Being natural means it is much more efficient than any other multiplications / divisions / modulo arithmetic, sometimes by orders of magnitude.

@MooingDuck 指出,计算平台的这种基本二进制性质已经遍及该语言及其标准,在某种程度上,试图构建一个非二进制符合的实现方式与在不削减gordian结的情况下取得同等的效果差不多.确实有少数几种计算机语言不正确.

As @MooingDuck points out, this fundamental binary nature of computing platforms has already pervaded the language and its standard to such an extent, trying to build a non-binary conforming implementation is about on-par with untying the gordian knot without just cutting it. There are really few computer languages where that's not true.

相关,维基百科上的字长对照表: http://zh-CN. wikipedia.org/wiki/Word_(计算机架构)#Table_of_word_sizes

Related, a table of word sizes on wikipedia: http://en.wikipedia.org/wiki/Word_(computer_architecture)#Table_of_word_sizes

这篇关于为什么对齐是2的幂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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