ISO IEC 14882-2011 §5.7 / 5 States:
If both pointer operands and the result point to elements of the same array object or one after the last element of the array object, the evaluation should not lead to overflow; otherwise, the behavior is undefined.
This section is used here in stackoverflow from time to time. For example, to state why the nullptr pointer increment is UB, for example here . Then it is interpreted as having a pointer that does not point to an element of an array object. This behavior is undefined.
However, when I read this, I realized that this refers to evaluating a pointer being UB. This would mean that having such a pointer is a well-defined behavior. And the behavior becomes undefined when you try to dereference it.
This means that, for example, incrementing a valid pointer outside the array boundary is legal. After that, its further reduction is legal. And since the pointer will be the same value as before the increment, the estimate is also legal.
Which of the two cases?
c ++ undefined-behavior language-lawyer semantics c ++ 11
laurisvr May 21 '15 at 9:11 a.m. 2015-05-21 09:11
source share