Sujet : Re: question about nullptr
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 09. Jul 2024, 03:45:19
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240708193402.610@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-07-08, James Kuyper <
jameskuyper@alumni.caltech.edu> wrote:
On 7/7/24 19:42, Ben Bacarisse wrote:
scott@slp53.sl.home (Scott Lurndal) writes:
...
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?
>
>
The problem is not "char p=0;". If you're sure what type p has, there's
no problem. The problem comes with code like "p=0", where "p=NULL" would
serve to remind you that p should be a pointer, while "p=nullptr"
guarantees a diagnostic if p is not a pointer.
The programming world has moved away from tying syntax to concrete
implementation though. For every time you worry in p = 0,
p is floating, integer or pointer, a thousand coders somewhere
are doing writing obj.foo(), where the type of obj can be one of
half a dozen different things at run-time.
p = 0 is nicely generic: "make p null, whatever it is".
A pointer "responds" to this "method" by becoming null.
And anyway, if the expression is p + 3 instead, what tells you then
whether it's an integer, float or pointer. Should we have a
ptrplus keyword for displacing pointers?
Ancient C, or pre-C, had separate operators for floating-point,
prefixed with a hash: #+, #-, #*, ...
I have fixed bugs which
would have been caught a lot earlier if nullptr had existed, and had
been the only permitted kind of null pointer constant.
All the ones I remember were about 0 being used as a variadic
pointer argument, without a cast. I can't think of anywhere else
it would be a problem.
nullptr being the only null pointer constant would prevent that
where the type is void * or char *, (or where pointers of all types all
have the same representation).
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca