Deutsch   English   Français   Italiano  
<87jzhwu5v9.fsf@bsb.me.uk>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Ben Bacarisse <ben@bsb.me.uk>
Newsgroups: comp.lang.c
Subject: Re: question about nullptr
Date: Mon, 08 Jul 2024 00:42:02 +0100
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <87jzhwu5v9.fsf@bsb.me.uk>
References: <v6bavg$3pu5i$1@dont-email.me> <20240706054641.175@kylheku.com>
	<v6bfi1$3qn4u$1@dont-email.me> <l9ciO.7$cr5e.2@fx05.iad>
	<877cdyuq0f.fsf@bsb.me.uk> <2ckiO.19403$7Ej.4487@fx46.iad>
	<87plrpt4du.fsf@bsb.me.uk> <9bCiO.7108$sXW9.3805@fx41.iad>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Mon, 08 Jul 2024 01:42:03 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f9b9b2152fe0e8376b284d605ac6478c";
	logging-data="571159"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+OdxhM/5prYw0pxw1QqiY/gOg8gUPPJ/U="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:oGmDZcYZ3uvE4Yvdnv6dJ9ZMEjQ=
	sha1:FoUfgrYPrpbdUDSHlHyI1fnwXQ0=
X-BSB-Auth: 1.70c039f7cc6c6b40b526.20240708004202BST.87jzhwu5v9.fsf@bsb.me.uk
Bytes: 4473

scott@slp53.sl.home (Scott Lurndal) writes:

> Ben Bacarisse <ben@bsb.me.uk> writes:
>>scott@slp53.sl.home (Scott Lurndal) writes:
>>
>>> Ben Bacarisse <ben@bsb.me.uk> writes:
>>>>scott@slp53.sl.home (Scott Lurndal) writes:
>>>>
>>>>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>>>>>On 06.07.2024 14:54, Kaz Kylheku wrote:
>>>>>>> On 2024-07-06, Thiago Adams <thiago.adams@gmail.com> wrote:
>>>>>>>> If you were creating C code today and could use a C23 compiler, would 
>>>>>>>> you use nullptr instead of NULL?
>>>>>>> 
>>>>>>> In greenfield projects under my dictatorship, I use 0, as in:
>>>>>>> 
>>>>>>>    char *p = 0;
>>>>>>> 
>>>>>>> I was still 20 something when I (easily) wrapped my head around the 0
>>>>>>> null pointer constant, and have not had any problems with it.
>>>>>>> Once I learned the standard-defined truth about null pointer constants,
>>>>>>> and their relationship to the NULL macro, I dropped NULL like a hot
>>>>>>> potato, and didn't look back (except when working in code bases that use
>>>>>>> NULL).
>>>>>>
>>>>>>We also used 0 as "universal" pointer value regularly without
>>>>>>problems.
>>>>
>>>>I also like to use 0, but I'm not sure I could say exactly why.  Maybe
>>>>because of pre-C exposure (B and BCPL).
>>>>
>>>>> Whereas I spent 6 years programming on an architecture[*] where a
>>>>> null pointer was represented in hardware by the value 0xc0eeeeee.  I always
>>>>> use the NULL macro in both C and C++ code.
>>>>
>>>>I'm sure you know (but maybe some other readers might not) that that
>>>>does not stop one using 0 in C source code.  Whatever a null pointer
>>>>"really" is on some hardware, 0 must work in C, including in comparisons
>>>>with == and !=.  You can have
>>>
>>> Yes.  However, I consider that ambiguous, I prefer to be explicit and
>>> use NULL or nullptr.
>>
>>In what sense is using 0 ambiguous?  I can't see it.
>
> the digit zero is context dependent, which makes it ambiguous.

Ah.  I thought we were talking about a pointer context so I thought you
meant that using 0 in a pointer context was ambiguous.  In

  char *p = 0;

the 0 is not ambiguous (i.e. open to more than one meaning).  It's open
to being misunderstood by people who don't know C, but that true of the
'char', the '*' and the '=' (and possibly the 'p' and the ';' too).

> I.e. it can either be a  null pointer or the value zero
> depending on context, which makes it ambiguous to the casual
> reader.  Particularly when reading code that someone
> else has written.  NULL makes the programmers intent crystal
> clear.

That's a rather niche readership -- one that might consider

  char *p = 0;

unclear.  Do you want such people reading your C code with a view to
working on it?

I find myself completely out of step with many posters here about
"explicit code" should look like.  I think

  char *p = 0;

is explicit enough and, in fact, I consider it a plus point if someone
reading it goes "hey, what's going on here?" and ends up learning that 0
is null pointer constant in C.  They may, along the way, learn a few
other things that they should also know before fiddling with the code.
At the very least, they will have learnt that they don't know it all.

-- 
Ben.