This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Can offsetting a non-null pointer result in a null one?
在 2018-08-20 16:27, Richard Biener 写道:
> Btw, I can't find wording in the standards that nullptr + 1 is
> invoking undefined behavior,
> that is, that pointer arithmetic is only allowed on pointers pointing
> to a valid object.
> Any specific pointers?
>
The C standard only defines addition and subtraction for pointers
pointing to elements and past-the-end positions of arrays [1]. As a null
pointer ipoints to nothing, the 'otherwise' clauses apply, rendering the
behavior undefined.
The C++ standard has a couple of similar paragraphs [2]. In addition to
that, there is a special case for null pointers and the integer zero
[3]. There is no special case for null pointers and integers other than
zero.
[1] ISO/IEC WG14 N1570, 6.5.6 Additive operators, 8, 9.
[2] ISO/IEC WG21 N4750, 8.5.6 Additive operators, 4, 5
[3] ISO/IEC WG21 N4750, 8.5.6 Additive operators, 7
--
Best regards,
LH_Mouse