Article View: gnu.g++.help
Article #787Re: NULL = ((void *)0) problem
From: James Kanze US/ESC 60/3/141 #407...
Date: Mon, 09 Oct 1995 11:04
Date: Mon, 09 Oct 1995 11:04
57 lines
3056 bytes
3056 bytes
In article <45666e$amf@engnews2.Eng.Sun.COM> clamage@Eng.Sun.COM
(Steve Clamage) writes:
|> 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.
I think, actually, that it is an ``integral constant expression
evaluating to 0'', which will be treated as a null pointer constant in
certain contexts.
|> Notice the difference between
|> a constant zero in source text and an integer with value zero in
|> machine code.
But this is exactly what Fergus was saying. There is already one
(very) special case; it wouldn't really take any more black magic to
make the constant expression ``(void*)0'' (or any expression which
evaluates to it) a special case, too.
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Technical Details
Message-ID:
<KANZE.95Oct9120444@slsvgqt.lts.sel.alcatel.de>Path:
arc1-ca.pugleaf.net!cett.alcatel-alsthom.fr!slsv6bt!newsReferences:
<435jf3$1iq@globe.indirect.com> <44vqv4$ael@s02.pavilion.co.uk> <4546nd$nnl@oldfart.ecl.wustl.edu> <9528023.17178@mulga.cs.mu.OZ.AU> <45666e$amf@engnews2.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help,comp.lang.c++ Path: arc1-ca.pugleaf.net!cett.alcatel-alsthom.fr!slsv6bt!news From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) Subject: Re: NULL = ((void *)0) problem In-Reply-To: clamage@Eng.Sun.COM's message of 7 Oct 1995 15:26:06 GMT Message-ID: <KANZE.95Oct9120444@slsvgqt.lts.sel.alcatel.de> Sender: news@lts.sel.alcatel.de Organization: SEL 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> <45666e$amf@engnews2.Eng.Sun.COM> Date: 09 Oct 1995 11:04:44 GMT