...
Similarly, comparing pointers using the relational operators <, <=, >=, and > gives the positions of the pointers relative to each other. Subtracting or comparing pointers that do not refer to the same array results in undefined behavior.
| Page properties | ||
|---|---|---|
| ||
This may be true for C, but is not true for C++ as best I can tell. [expr.rel]p3 says:
[expr.rel]p4 says:
The way I read this is that if two pointers do not fall into one of the three categories from p3, then we fall into the "Otherwise" clause of p4, and so the result is unspecified (not undefined). Another interesting (pedantic) reading is that two pointers that point to one past the last element of the same array cannot be compared with the relational operator without exhibiting unspecified behavior. Eg)
This might be something to bring up in CWG. |
Comparing pointers using the equality operators == and != has well-defined semantics regardless of whether or not either of the pointers is null, points into the same object, or points one past the last element of an array object or function.
...
[Banahan 03] Section 5.3, "Pointers," and Section 5.7, "Expressions involving pointers"
[ISO/IEC 14882-2003] Section 5.7 "Additive operators"
[MITRE 07] CWE ID 469, "Use of Pointer Subtraction to Determine Size"
...