C空指针算法 [英] C null pointer arithmetic

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

问题描述

我注意到了来自Clang的警告:

I noticed this warning from Clang:

warning: performing pointer arithmetic on a null pointer
has undefined behavior [-Wnull-pointer-arithmetic]

详细来说,正是此代码触发了此警告:

In details, it is this code which triggers this warning:

uint8_t *end = ((uint8_t*)0) + sizeof(uint8_t) * count;

为什么对从非零整数获取的非空指针执行相同操作时,禁止对空指针进行算术运算不会触发任何警告?

Why would arithmetic on a null pointer be forbidden when doing the same on a non-null pointer obtained from an integer different than zero does not trigger any warning ?

更重要的是,C标准是否明确禁止空指针算法?

And more importantly, does the C standard explicitly forbid null pointer arithmetic ?

推荐答案

C标准不允许这样做.

The C standard does not allow it.

6.5.6加法运算符(强调我的意思)

6.5.6 Additive operators (emphasis mine)

8 当具有整数类型的表达式为添加到或 从指针中减去,结果具有指针的类型 操作数. 如果指针操作数指向数组的元素 对象,并且数组足够大,结果指向一个元素 与原始元素的偏移量,使得 结果数组元素和原始数组元素的下标等于 整数表达式.换句话说,如果表达式P指向 数组对象的第i个元素,表达式(P)+ N(等效地, N +(P))和(P)-N(其中N的值为n)分别指向 数组对象的第i + n个元素和第i-n个元素(如果存在). 此外,如果表达式P指向数组的最后一个元素 对象,表达式(P)+1指向对象的最后一个元素 数组对象,如果表达式Q指向最后一个元素之后的一个 数组对象的表达式(Q)-1指向数组对象的最后一个元素 数组对象. 如果指针操作数和结果都指向 相同数组对象的元素,或者位于最后一个元素之后的元素 数组对象,求值不得产生溢出;否则, 行为是不确定的.如果结果指向最后一个 数组对象的元素,不得将其用作a的操作数 一元*运算符.

8 When an expression that has integer 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 integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i-n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

出于上述目的,指向单个对象的指针被视为指向1个元素的数组.

For the purposes of the above, a pointer to a single object is considered as pointing into an array of 1 element.

现在,((uint8_t*)0) 没有指向数组对象的元素.仅仅因为持有空指针值的指针不会指向任何对象.在以下位置说:

Now, ((uint8_t*)0) does not point at an element of an array object. Simply because a pointer holding a null pointer value does not point at any object. Which is said at:

6.3.2.3指针

3 如果转换了空指针常量指针类型, 结果指针(称为空指针)可以保证进行比较 不等于指向任何对象或函数的指针.

3 If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

所以您不能对此进行算术运算.该警告是合理的,因为正如第二个突出显示的句子所提到的,我们处于行为未定义的情况.

So you can't do arithmetic on it. The warning is justified, because as the second highlighted sentence mentions, we are in the case of undefined behavior.

不要被offsetof宏可能是这样实现的事实所迷惑.标准库不受用户程序约束的约束.它可以使用更深入的知识.但是在我们的代码中这样做的定义不明确.

Don't be fooled by the fact the offsetof macro is possibly implemented like that. The standard library is not bound by the constraints placed on user programs. It can employ deeper knowledge. But doing this in our code is not well defined.

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

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