Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Feb 1997 02:55:07 +0100
From:      Eivind Eklund <eivind@dimaga.com>
To:        hackers@FreeBSD.ORG (Joerg Wunsch)
Subject:   Re: NULL as ((void*)0) (was Re: strlen() question)
Message-ID:  <3.0.32.19970215025505.00c3f100@dimaga.com>

next in thread | raw e-mail | index | archive | help
At 12:26 AM 2/15/97 +0100, J Wunsch wrote:
>As Eivind Eklund wrote:
>
>> I hereby propose changing the default declaration of NULL under FreeBSD
from
>> #define NULL 0
>>  to
>> #define NULL ((void*)0)
>>  for better type-safety and ease of transition to other architechtures
>> (e.g. Alpha).   This will probably save us from a quite a few varargs-voes,
>> as well as generally making sure the code-base is using NULL correctly,
>> which is important for those reading source-code.
>
>Nope, it wouldn't get you anywhere.  The vararg-woes cannot be solved
>by it either, since you gotta cast to the exact type in any case
>anyway.  Passing (void *)0 into a vararg list is as invalid as passing
>0 there if the target type is e.g. char *.

It isn't invalid except at the virtual machine level - it invoke undefined
behaviour.  In many cases (e.g. FreeBSD today) even passing an uncasted 0
works.  In all cases, a void* passed instead of a char* will work (I can
dig up chapter and verse for this one - bad example :)  Passing void* for
eg int* involve undefined behaviour, though.  But on 99% of todays
available hardware (excluding eg Prime and older Crays) pointers to
different typed objects have the same size and the same internal structure.
 For this case, it will work.  And in no case will it work worse[1].

>It wouldn't ensure the code base is using NULL everywhere either,

This wasn't what I was thinking of - quite the opposite.  I was thinking of
making sure the code base wasn't abusing NULL in a non-pointer context, and
make programming environment somewhat more precise for beginners.

>See also the comp.lang.c FAQ.

I have done, repeatedly and at regular intervals.  comp.std.c is also to be
recommended for those wanting a nitpicker or compiler-writer view of C.

>The only reason to use NULL is to make it more obvious to the reader
>that you're thinking of a pointer context.

Agreed.

[1] There has come up one serious objection here, relating to C++.  If this
objection is correct, it mean that the proposal absolutely should be
dropped.  Besides, nobody but me seems to be in favour, and it isn't
important to me, so...


Eivind Eklund  perhaps@yes.no  http://maybe.yes.no/perhaps/
eivind@freebsd.org




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3.0.32.19970215025505.00c3f100>