C++ Logo

liaison

Advanced search

Re: [wg14/wg21 liaison] nullptr + 0

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Thu, 26 Oct 2023 14:32:58 +0100
On Thu, 26 Oct 2023, 14:06 Thomas Köppe via Liaison, <
liaison_at_[hidden]> wrote:

> On that note, a pet peeve of mine is when people say "nullptr" when they
> mean to say "null pointer". I have no idea what gives them this idea (did
> we pick a terrible name?), but even experts and committee members do this.
> I regularly point out that nullptr is neither null nor a pointer. It sounds
> like this may be exactly the kind of confusion here?
>

Yes, I think so. Let's all assume Robert meant (T*)nullptr + 0 for a
non-void T.


> Allowing + 0 on a null pointer has the pleasant ramification that [p, p +
> N) is a valid range even when p is null and N is 0, which is quite handy in
> C++.
>

Indeed. Contrast this with memcpy, memset etc where you get UB if a pointer
argument is null, even if n=0. This is a frequent source of bugs, and users
are frequently surprised that memcpy(p1, p2, 0) is not a no-op for null p1
or p2, because it can cause compilers to remove null checks and make other
surprising optimisations.

Allowing p+0 for all p, even null pointers, avoids such surprises elsewhere.



> On Thu, 26 Oct 2023 at 14:00, JeanHeyd Meneide via Liaison <
> liaison_at_[hidden]> wrote:
>
>> On Thu, Oct 26, 2023 at 8:41 AM Jens Maurer via Liaison <
>> liaison_at_[hidden]> wrote:
>>
>>>
>>> On 26/10/2023 14.29, Robert Seacord via Liaison wrote:
>>> >
>>> > Does anyone know why |nullptr + 0| is defined in C++ but not C?
>>>
>>> What makes you believe "nullptr + 0" to be well-formed in C++?
>>>
>>> At least two compilers (gcc, clang) agree it's not:
>>>
>>> x.cc:4:22: error: invalid operands of types ‘std::nullptr_t’ and ‘int’
>>> to binary ‘operator+’
>>> 4 | void * p = nullptr + 0;
>>> | ~~~~~~~~^~~
>>>
>>
>> Having had this conversation in C before, I'm going to presume he means
>> not the literal null pointer but the clause in [expr.add] ¶4.1 that
>> explicits exempts it from undefined behavior when used on any pointer whose
>> value is the null pointer with 0:
>>
>> When an expression J that has integral type is added to or subtracted
>> from an expression P of pointer type, the result has the type of P:
>> — If P evaluates to a null pointer value and J evaluates to 0, the
>> result is a null pointer value.
>> …
>>
>> This is indeed different from C, where there is no carve out for
>> this case in particular.
>>
>> Sincerely,
>> JeanHeyd
>> _______________________________________________
>> Liaison mailing list
>> Liaison_at_[hidden]
>> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/liaison
>> Link to this post: http://lists.isocpp.org/liaison/2023/10/1324.php
>>
> _______________________________________________
> Liaison mailing list
> Liaison_at_[hidden]
> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/liaison
> Link to this post: http://lists.isocpp.org/liaison/2023/10/1325.php
>

Received on 2023-10-26 13:33:19