🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #783

Re: NULL = ((void *)0) problem

#783
From: Steve Clamage
Date: Sat, 07 Oct 1995 15:26
40 lines
2151 bytes
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:

>jaf3@motel6.cec.wustl.edu (Stephen W. Gevers) writes:

>>Andy J Robb <AJRobb@pavilion.co.uk> wrote:
>>:Ron Forrester <rjf@infograph.com> wrote:
>>:...
>>:It is madness if ANSI C++ allows implict casting of an integral value
>>:of a different size (zero) to a pointer but NOT ((void*)0).
>>
>>The reason that you cannot implicitly cast (void*) 0 to a null pointer
>>is because it would be a type hole (one that C currently allows):

>If that is the reason, then it is a bogus reason.
>Allowing `(void *)0' to be implicitly converted to a null
>pointer does not create any type holes.

This topic is discussed in D&E. Everywhere else in C++, the validity
of a cast is determined only by the types, not by the values. (With
one exception noted in a moment.) If we allow
	T* p = (void*)0;
then why not
	void* q = 0;
	T* p = q;
Carrying this argument to its logical conclusion you should allow
	extern void* q;
	T* p = q
if q is null and not otherwise, requiring a runtime check. In fact,
you can get this effect using a dynamic_cast, which is always valid
for null pointers, and which is the cast that takes values into account.

Implicit casts are valid or not based only on the types, not the values.

I also want to address Forrester's comment about converting an integer
to a pointer. That is NOT the case. A literal zero in a pointer context
is not an integer, but a null pointer. Notice the difference between
a constant zero in source text and an integer with value zero in
machine code.
--
Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID: <45666e$amf@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!clamage
References: <435jf3$1iq@globe.indirect.com> <44vqv4$ael@s02.pavilion.co.uk> <4546nd$nnl@oldfart.ecl.wustl.edu> <9528023.17178@mulga.cs.mu.OZ.AU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!clamage
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re: NULL = ((void *)0) problem
Date: 7 Oct 1995 15:26:06 GMT
Organization: Sun Microsystems Inc., Mountain View, CA
Message-ID: <45666e$amf@engnews2.Eng.Sun.COM>
References: <435jf3$1iq@globe.indirect.com> <44vqv4$ael@s02.pavilion.co.uk> <4546nd$nnl@oldfart.ecl.wustl.edu> <9528023.17178@mulga.cs.mu.OZ.AU>
NNTP-Posting-Host: taumet.eng.sun.com
X-Newsreader: NN version 6.5.0 #21 (NOV)