是否增加一个明确定义的空指针? [英] Is incrementing a null pointer well-defined?

查看:133
本文介绍了是否增加一个明确定义的空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多未定义/未指定行为的例子,当做指针算术 - 指针必须指向同一数组(或一个超过结束),或内部同一对象,限制,当你可以做比较/操作以上等等。

There are lots of examples of undefined/unspecified behavior when doing pointer arithmetics - pointers have to point inside the same array (or one past the end), or inside the same object, restrictions on when you can do comparisons/operations based on the above, etc.

以下操作是否明确定义?

Is the following operation well-defined?

int* p = 0;
p++;


推荐答案

§5.2.6/ 1:


通过向 1 添加操作数对象的值来修改操作数对象的值,除非对象是类型 bool [..]

The value of the operand object is modified by adding 1 to it, unless the object is of type bool [..]

§5.7/ 5:


如果指针操作数和结果指向
的元素同一个数组对象,超过数组对象的最后一个元素,
,计算不会产生溢出; 否则,
的行为未定义。

这篇关于是否增加一个明确定义的空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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