From -6196153726978869447
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,f1dec9ce20a9c98f
X-Google-Attributes: gidf78e5,public
From: David R Tribble <david@tribble.com>
Subject: Re: null pointers (was: Why's of C++ -- Part 1)
Date: 1999/08/14
Message-ID: <37B48FBA.2DE77379@tribble.com>#1/1
X-Deja-AN: 512555421
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <7opqvm$vpa$1@news.hal-pc.org> <HJis3.13$m84.269@burlma1-snr2> <7p0mu0$36f$1@mulga.cs.mu.OZ.AU> <FGEw80.FB0@research.att.com>
X-Original-Date: Fri, 13 Aug 1999 16:35:54 -0500
X-Accept-Language: en
X-Authentication-Warning: backdraft.briar.org: smap set sender to <news@beasys.com> using -f
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 934618929 1695 128.250.29.17 (14 Aug 1999 08:22:09 GMT)
Organization: The Vast Right-Wing Conspiracy
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAN7UnGeEDnX0m9pzZAQEH0AF8CfteyEH5NHV5zBpX2KFOXq8JG75qvHk5 sBd0E19h0DezHGndksfJMxRDYhjlhDXL =jbdO
Mime-Version: 1.0
NNTP-Posting-Date: 14 Aug 1999 08:22:09 GMT
Newsgroups: comp.std.c++

Fergus Henderson wrote:
>> IMHO the change to disallow `(void *)0' as a null pointer
>> constant in C++ was just a bad decision.

Pete Becker wrote:
> There was no such decision. It is a consequence of the more general
> decision (made back in the dawn of time) that pointers to void are not
> implicitly convertible to other pointer types.

Bjarne Stroustrup wrote:
> Please note that I did not make a decision to change C++ in a way that
> was incompatible with C; nor did the ISO C++ committee. There simply
> was no change. The null pointer in K&R C was defined exactly as in is
> now in C++. At some point in its work, the ANSI C committee gave
> void* a very special semantics (primarily) to allow things like
> 
>     struct S *p = malloc(sizeof(struct C);
> 
> rather than requiring a cast as in K&R C (and C++):
> 
>     struct S *p = (struct S *)malloc(sizeof(struct C);
> 
> This relaxation of the type system wasn't necessary in C++ (given
> new) and opens a type hole, so I didn't follow suit. By the time
> ANSI C was approved (with the void* type hole), C++ had been defined
> and used without that hole and compatible with C for about five
> years.
> 
> At some point, it was discovered that given ANSI C type rules,
> benefits could be had by using (void*)0 as the null pointer.
> 
> I think a good argument can be made for introducing a null pointer
> (with some suitable notation) into C++, but I don't see a good 
> argument for simply adopting the void* hole in the type system from
> C.
> 
> The following should be rejected by a compiler:
> 
>      char c;
>      char* pc = &c;
>      void* pv = pc;  // legal C, not legal C++
>      int* pi = pv;
>      int i = *pi;

But '(void*)0' could have been deemed a special case in C++, so that
it would be treated like a true generic null pointer constant, and
not simply as a pointer-to-void.  This special case could have been
made without opening up the while void* type hole.

Given this special handling of '(void*)0', all of the statements
above would still be illegal, but the following statements would now
be legal in both C and C++:

    char * pc = (void*)0;
    void * pv = (void*)0;
    int *  pi = (void*)0;

The benefit of this approach over other proposal (such as adding
a 'null' or '__null__' keyword, or inventing new syntax like '&0')
is that it doesn't affect any existing C++ or C code.  It's also a
pretty trivial change to make to the standard.

-- David R. Tribble, david@tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



