非数组对象类型的单末尾指针可以吗? [英] Is one-past-end pointer OK for non-array object types?

查看:78
本文介绍了非数组对象类型的单末尾指针可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效的C ++吗?

Is this valid C++?

int main()
{
    int i = 0;
    int* pi = &i;
    ++pi;
}

我知道数组类型允许使用单尾指针,但是在这种情况下我不确定.从技术上讲,该代码是否具有未定义的行为?

I know that one-past-end pointers are allowed for array types, but I'm not sure in this case. Does that code technically have undefined behavior?

推荐答案

是的,可以.它是任何指针类型都可以容纳的四类值之一.

Yes, it is okay. It is one of the four categories of values any pointer type may hold.

[basic.compound] (强调我的意思)

[basic.compound] (emphasis mine)

3 指针类型的每个值都是以下值之一:

3 Every value of pointer type is one of the following:

  • 指向对象或函数的指针(据说该指针指向该对象或函数),或者
  • 超出对象末尾的指针([expr.add]),或
  • 该类型的空指针值([conv.ptr]),或
  • 无效的指针值.
  • a pointer to an object or function (the pointer is said to point to the object or function), or
  • a pointer past the end of an object ([expr.add]), or
  • the null pointer value ([conv.ptr]) for that type, or
  • an invalid pointer value.

指针类型的值,该值是指向或超过结束符的指针 对象代表内存中第一个字节的地址 ([intro.memory])由对象或内存中的第一个字节占用 结束存储后分别由该对象占用. [ 注意:指向对象([expr.add])末尾的指针不是 被认为指向对象类型不相关的对象 可能位于该地址.指针值在以下情况下变为无效 它表示的存储已达到其存储期限的末尾;看 [basic.stc]. —尾注] 用于指针算术 ([expr.add])和比较([expr.rel],[expr.eq]),指针经过 n个元素的数组x的最后一个元素的末尾被认为 等效于x的假设数组元素n的指针 并且类型T的对象(不是数组元素)被认为是 属于一个类型为T的元素的数组.

A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory ([intro.memory]) occupied by the object or the first byte in memory after the end of the storage occupied by the object, respectively. [ Note: A pointer past the end of an object ([expr.add]) is not considered to point to an unrelated object of the object's type that might be located at that address. A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration; see [basic.stc]. — end note ] For purposes of pointer arithmetic ([expr.add]) and comparison ([expr.rel], [expr.eq]), a pointer past the end of the last element of an array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x and an object of type T that is not an array element is considered to belong to an array with one element of type T.

正如您所看到的,这里还提到了数组类型,其假设对象在末尾.正如[expr.add] 中的脚注所说明的那样,获得唯一指针的算法 meant 也有效:

As you can see, array types are also mentioned here, with their hypothetical object past the end. And as the footnote in [expr.add] explains, the arithmetic to obtain a one-past-the-end pointer is meant to be valid too:

如[basic.compound]中所指定,为此目的,不是数组元素的对象被视为属于单元素数组,并且经过n个元素的数组的最后一个元素的指针被认为是等效于为此目的指向假设数组元素n的指针.

As specified in [basic.compound], an object that is not an array element is considered to belong to a single-element array for this purpose and a pointer past the last element of an array of n elements is considered to be equivalent to a pointer to a hypothetical array element n for this purpose.

这篇关于非数组对象类型的单末尾指针可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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