指针算术和积分提升 [英] Pointer arithmetic and integral promotion

查看:108
本文介绍了指针算术和积分提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表达式p + a中,其中p是指针类型,而a是整数,是否适用整数提升规则?例如,如果achar,则在64位计算机上,在将其添加到指针值(在已编译程序集中)之前,它一定会扩展为64位,但是它是由标准指定的吗?它会被推广到什么? intintptr_tptrdiff_t? unsigned charsize_t将被转换为什么?

In the expression p + a where p is a pointer type and a is an integer, will integer promotion rules apply? For example, if a is a char, on a 64-bit machine it will surely be extended to 64 bit before being added to the pointer value (in the compiled assembly), but is it specified by the standards? What will it be promoted to? int, intptr_t or ptrdiff_t? What will unsigned char or size_t be converted to?

推荐答案

C ++ 11§5.7/1:

C++11 §5.7/1:

<加法运算符+-从左到右分组.通常对算术或枚举类型的操作数执行算术转换.

“The additive operators + and - group left-to-right. The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.”

这显然可以减少考虑由…

This apparently reduces the problem to considering the usual arithmetic conversions, defined by …

C ++ 11§5/9:

C++11 §5/9:

“许多期望算术或 枚举类型原因转换和收益结果类型类似 道路.目的是产生一个通用类型,这也是 结果这种模式称为通常的算术转换, 定义如下:

“Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result This pattern is called the usual arithmetic conversions, which are defined as follows:

  • 如果任何一个操作数都属于范围枚举类型(7.2),则不执行任何转换;否则,不执行任何转换.如果otheroperand不具有相同的类型,则表达式格式不正确.

  • If either operand is of scoped enumeration type (7.2), no conversions are performed; if the otheroperand does not have the same type, the expression is ill-formed.

如果其中一个操作数的类型为long double,则另一个应转换为long double.

If either operand is of type long double, the other shall be converted to long double.

否则,如果其中一个操作数为double,则另一个应转换为double.

Otherwise, if either operand is double, the other shall be converted to double.

否则,如果其中一个操作数为float,则另一个应转换为float.

Otherwise, if either operand is float, the other shall be converted to float.

否则,应在两个操作数上执行积分提升(4.5).然后,将以下规则应用于提升后的操作数:

Otherwise, the integral promotions (4.5) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

  • 如果两个操作数具有相同的类型,则无需进一步转换.

  • If both operands have the same type, no further conversion is needed.

否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则将具有较小整数转换等级的操作数转换为具有较高等级的操作数的类型.

Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.

否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则应将具有符号整数类型的操作数转换为具有无符号整数的操作数的类型整数类型.

Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.

否则,如果带符号整数类型的操作数的类型可以表示带无符号整数类型的操作数的所有值,则应将带无符号整数类型的操作数转换为操作数的类型有符号整数类型.

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.

否则,两个操作数都应转换为与带符号整数类型的操作数类型相对应的无符号整数类型.

Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.”

按照机械规则,这组规则将以最后一个项目符号点(标准中的破折号)结束,并将指针操作数转换为与不存在的对象相对应的无符号整数类型.这是错误的.因此,措辞通常对算术或枚举类型的操作数执行算术转换".不能从字面上解释-ndash;这是恕我直言的缺陷–但必须将其解释为通常的算术转换是针对两个操作数均为算术或枚举类型的调用执行的"

Followed mechanically, this set of rules would end up in the last bullet point (dash in the standard) and convert a pointer operand to the unsigned integer-type corresponding to something non-existing. Which is just wrong. So the wording “The usual arithmetic conversions are performed for operands of arithmetic or enumeration type” can not be interpreted literally – it's IMHO defective – but must be interpreted like “The usual arithmetic conversions are performed for invocations where both operands are of arithmetic or enumeration type“

因此,通过通常的算术转换调用的促销就不会在一个操作数是指针的情况下起作用.

So, promotions as such, which are invoked via the usual arithmetic conversions, don't come into play when one operand is a pointer.

但是在第5.7节中,有人找到了…

But a bit further down in §5.7 one finds …

C ++ 11§5.7/5:

C++11 §5.7/5:

“当将具有整数类型的表达式添加到指针或从指针中减去时,结果将具有指针操作数的类型.如果指针操作数指向数组对象的元素,并且数组足够大,则结果指向与原始元素偏移的元素,以使结果数组元素和原始数组元素的下标之差等于整数表达式. ”

“When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression.”

这完全根据数组索引来定义结果.对于char数组,下标的差异可以超过ptrdiff_t的范围.一个实现的合理安排是将非指针参数转换为无符号整数类型size_t(在位级别有效地对符号进行扩展),然后将该值与模块化算法一起使用以计算结果指针值.

This defines the result entirely in terms of array indexing. For a char array the difference of subscripts can exceed the range of ptrdiff_t. A reasonable way for an implementation to arrange this, is to convert the non-pointer argument to the unsigned integral type size_t (effectively sign extension at the bit level), and use that value with modular arithmetic to compute the resulting pointer value.

这篇关于指针算术和积分提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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