C ++中的指针比较是未定义的还是未指定的行为? [英] Is pointer comparison undefined or unspecified behavior in C++?

查看:122
本文介绍了C ++中的指针比较是未定义的还是未指定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stroustrup编写的C ++编程语言第三版说,

The C++ Programming Language 3rd edition by Stroustrup says that,

仅当两个指针都指向时定义指针的减法 同一数组的元素(尽管语言没有快速的方法 确保是这种情况).当从另一个指针中减去一个指针时, 结果是两个指针之间的数组元素数 (一个整数).可以向指针添加整数或减去 指针的整数;在两种情况下,结果都是一个指针值. 如果该值未指向与 原始指针或其他指针,则使用该值的结果是 未定义.

Subtraction of pointers is defined only when both pointers point to elements of the same array (although the language has no fast way of ensuring that is the case). When subtracting one pointer from another, the result is the number of array elements between the two pointers (an integer). One can add an integer to a pointer or subtract an integer from a pointer; in both cases, the result is a pointer value. If that value does not point to an element of the same array as the original pointer or one beyond, the result of using that value is undefined.

例如:

void f ()
{
    int v1 [10];
    int v2 [10];
    int i1 = &v1[5] - &v1[3];   // i1 = 2
    int i2 = &v1[5] - &v2[3];   // result undefined
}

我在Wikipedia上阅读有关未指定行为的信息.它说

I was reading about unspecified behavior on Wikipedia. It says that

在C和C ++中,仅当指针指向同一对象的成员或同一数组的元素时,才严格定义对象的指针比较.

In C and C++, the comparison of pointers to objects is only strictly defined if the pointers point to members of the same object, or elements of the same array.

示例:

int main(void)
{
  int a = 0;
  int b = 0;
  return &a < &b; /* unspecified behavior in C++, undefined in C */
}

所以,我很困惑.哪一个是正确的?维基百科还是Stroustrup的书?对此,C ++标准怎么说?

So, I am confused. Which one is correct? Wikipedia or Stroustrup's book? What C++ standard says about this?

如果我误会我的话,纠正我.

Correct me If I am misunderstanding something.

推荐答案

请注意,指针减法和指针比较是具有不同规则的不同操作.

Note that pointer subtraction and pointer comparison are different operations with different rules.

C ++ 14 5.6/6,减去指针:

C++14 5.6/6, on subtracting pointers:

除非两个指针都指向同一数组对象的元素或指向数组对象最后一个元素的指针,否则行为是不确定的.

Unless both pointers point to elements of the same array object or one past the last element of the array object, the behavior is undefined.

C ++ 14 5.9/3-4:

C++14 5.9/3-4:

将对象的指针进行比较的定义如下:

Comparing pointers to objects is defined as follows:

  • 如果两个指针指向同一数组的不同元素或其子对象,则下标较高的元素的指针比较大.

  • If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.

如果一个指针指向数组的元素或其子对象,而另一个指针指向数组的最后一个元素,则后者的指针比较大.

If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.

如果两个指针递归指向同一对象的不同非静态数据成员或此类成员的子对象,则递归地指向后面声明的成员的指针比较大,前提是两个成员具有相同的访问控制,并且前提是他们的班级不是工会.

If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control and provided their class is not a union.

如果两个操作数pq比较等于(5.10),则p<=qp>=q都产生true,而p<qp>q都产生false.否则,如果指针p的比较结果大于指针qp>=qp>qq<=pq<p,则全部产生true,以及p<=qp<qq>p均产生false.否则,未指定每个运算符的结果.

If two operands p and q compare equal (5.10), p<=q and p>=q both yield true and p<q and p>q both yield false. Otherwise, if a pointer p compares greater than a pointer q, p>=q, p>q, q<=p, and q<p all yield true, and p<=q, p<q, q>=p, and q>p all yield false. Otherwise, the result of each of the operators is unspecified.

这篇关于C ++中的指针比较是未定义的还是未指定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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