By Destructor


2015-08-02 17:37:26 8 Comments

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.

For example:

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

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

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.

Example:

int main(void)
{
  int a = 0;
  int b = 0;
  return &a < &b; /* unspecified behavior in C++, undefined in 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.

1 comments

@aschepler 2015-08-02 17:50:39

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

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:

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.

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.

@Lorenzo Belli 2016-06-02 12:48:23

In the last sentence "Otherwise, the result of each of the operators is unspecified." Why should it be unspecified instead of undefined? is it because the standard defines that the possible outcomes are only True of False so no other behaviours can happen?

@Mark Ransom 2016-11-21 15:48:45

@LorenzoBelli "undefined" is a much more serious error than "unspecified", literally anything can happen including crashing your program. With unspecified behavior, the only problem is that you don't know whether the result will be true or false - and the compiler is free to specify that behavior more rigorously than the standard requires. Generally the spec writers want to allow well behaved programs, and only specify undefined behavior when absolutely necessary.

@Ajay Brahmakshatriya 2017-09-14 05:36:08

@LorenzoBelli Also, the operator has to return the same value if the comparison is done twice in case of Unspecified behavior. But with UB, every comparison can return true/false independently.

@supercat 2018-06-18 22:50:53

@AjayBrahmakshatriya: There is no requirement that operations that yield Unspecified results do so consistently. Given int a=p>q, b=p>q, c=a;, if p and q identify unrelated objects, a compiler could make a and b be any combination of the values 0 and 1. A compiler, however, would be required to make c and a be equal.

Related Questions

Sponsored Content

37 Answered Questions

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 296813 View
  • 1527 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

9 Answered Questions

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2236394 View
  • 4247 Score
  • 1 Answer
  • Tags:   c++ c++-faq

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

5 Answered Questions

[SOLVED] Undefined behavior and sequence points

19 Answered Questions

5 Answered Questions

11 Answered Questions

[SOLVED] Why is f(i = -1, i = -1) undefined behavior?

Sponsored Content