From -6299027145692160638
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,dfa6d585499f3572
X-Google-Attributes: gidf78e5,public
From: David R Tribble <dtribble@technologist.com>
Subject: Re: delete (T*)0;
Date: 1999/06/23
Message-ID: <37700B87.F6085644@technologist.com>#1/1
X-Deja-AN: 492858479
Content-Transfer-Encoding: 7bit
Approved: Valentin Bonnard <bonnard@clipper.ens.fr>
References: <oN6b3.1735$ec.32152@ndnws01.ne.mediaone.net>
X-Original-Date: Tue, 22 Jun 1999 17:17:43 -0500
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Member of the Vast Right-Wing Conspiracy
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUAN3CYU6wEuYhIxRhxAQFAjAH/X1VVoyG1Axjj9nYhZlrdgQMdlM/aJjl+ 71Sa3jUj4Tct5o+95PacsMt6w3Ko7rWmmXmTh394jqd47rh3MWa6vQ== =tiJQ
Mime-Version: 1.0
Newsgroups: comp.std.c++

Dave Abrahams wrote:
> I'm trying to find out whether, when calling delete on a null pointer 
> value, the implementation is allowed to call operator delete anyway.
> I guess that it is legal for a function which is required to have
> "no effects" to call another function which has no effects, and
> operator delete is not allowed to have effects when called with a
> null pointer, so the answer is yes.
> 
> Just how strongly is "no effect" indended?

I've always been under the impression that the following should
always work:

    void foo()
    {
        int *  p = new int;

        delete p;
        p = 0;
        delete p;   // Guaranteed to work, no effect
    }

Since this code calls the global '::operator delete()', I am led
to the conclusion that calling delete on a null pointer will always
be safe.  In other words, it is okay to go ahead and call delete on
a null pointer.

On the other hand, it appears to be unspecified whether the global
'::operator delete()' function is actually called if the delete
operand is null; it would seem to be reasonable to allow the compiler
to generate code to check for null in order to avoid making the
call.  One way to find out would be to supply your own global
delete() and see what happens.  In any case, it's a bad idea not to
check for null in user-written delete() functions; failing to do so
would appear to violate the guarantee.

-- David R. Tribble, dtribble@technologist.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              ]



