From -1029196786835884244
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,176c9dc06a6729eb
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-03-24 20:45:01 PST
Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "James Kuyper Jr." <kuyper@wizard.net>
Newsgroups: comp.std.c++
Subject: Re: why doesn't delete NULL the pointer?
Date: Sun, 24 Mar 2002 22:41:26 CST
Organization: Giganews.Com - Premium News Outsourcing
Lines: 56
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <3C9DD259.621506AB@wizard.net>
References: <a7j7vg$78s$1@watserv3.uwaterloo.ca>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Accept-Language: en
NNTP-Posting-Date: Sun, 24 Mar 2002 07:10:23 CST
X-Trace: sv3-mZYbuqgivqWzASXSnYGviSjK8CUpPhHtGFgdFcOOYmGqbDjh1Ut6EvN13aO2xZQZj7AO4DUaDN4sc78!HALfY+v7e/4SyteF0/QxGjbcFfSCFyEcVTGp7bUTtu19Vph4aMXSbEzh0XYJ
X-Complaints-To: abuse@GigaNews.Com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-Info: Please be sure to forward a copy of ALL headers
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
Xref: archiver1.google.com comp.std.c++:10161

Pedro Sam wrote:
> 
> Hi all,
> 
> I'm curious about the delete operator and NULL pointers.  Why doesn't
> the standard require that the delete operator to set the pointer's value
> to NULL?
> 
> If the pointer is NULL, then if someone comes along and try to access
> that pointer, it would crash with a segfault, instead of reading garbage
> ( or even worse, write over some memory ).
> 
> Crash early is a Good Thing(TM) right?
> 
> I would appreciate any comments.
> 
> p2sam

There's an old principle that goes back to the design of C, and
continues into C++: you don't pay for what you don't use. Most of the
time, well-designed code doesn't need to null the pointer, not even as a
safety precaution. I usually try to arrange it that a pointer goes out
of scope immediately after freeing the memory it points at; it's not
very difficult. Therefore, the (admittedly small) amount of time needed
to null the pointer would be wasted. Code that needs the pointer to be
nulled can do so explicitly. If you need that feature a lot, it's
trivial to write a template wrapper for delete that will do it
automatically.

Now, this is a very minor matter, and it might have been changed when
C++ supplemented free() with the 'delete' operator. However, C++ code
generally makes far less explicit use of dynamic memory allocation than
C. You should be using containers, and certain kinds of smart pointers,
which handle the allocation in an exception-safe manner, rather that
doing it yourself, and having to worry about making sure that exceptions
don't prevent the 'delete' from occurring.

<pedantic>
The word NULL, when written entirely in capital letters, should be
treated as a noun, not a verb or an adjective. It's the name of a
particular stardard library macro. The word 'null', in lower case, it
the appropriate one to use in all other cases. In particular, it's not
possible for a pointer value to be NULL; NULL is required to be a null
pointer constant (NPC), and NPCs are required to have an integer type,
not a pointer type. A pointer value can be null, and such a pointer will
always compare equal to NULL, but that's because of the special rules
for equality comparisons involving NPCs, not because the pointer value
is NULL.
</pedantic>

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]



