Topic: Deleting pointers more than once (Related Question)


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Wed, 8 Dec 1993 10:09:53 GMT
Raw View
In article <CH9Fq3.27u@cobra.cs.unm.edu> shapiro@cobra.cs.unm.edu (Henry Shapiro) writes:
>This discussion of deleting a pointer more than once brings up a related
>question.  The "standard" says...

There is no "standard" yet (either with or without double quotes).

>... that  delete ptr  is harmless if  ptr == 0 .

The closest thing to a standard that now exists is the C++ standardization
committee's working paper... and I can find no such rule in the 09/28/93
version of that document... at least not in the section describing the
delete operator (i.e. section 5.3.4).

Even is such a rule *were* present there, I think that I would have to
say that the exact meaning of the word "harmless" in this context is
more than a little bit ambiguous.  "Harmless" to whom?

>The background to this question is:
>  (1) ptr was declared  X* p  where  X  is a class.
>  (2) class X overloads  delete .
>  (3) class X has a destructor.
>All the compilers I have easy access to when executing  delete ptr  with
> ptr == 0  appear to do "nothing", i.e., they certainly do not/cannot call
>the destructor on the object to which  p  points, since it doesn't point
>to an object.  BUT IT ALSO DOES NOT CALL THE CODE FOR THE OVERLOADED delete.
>The "standard" does not seem to address the issue of whether it should or
>should not (or is implementation dependent :-( heaven forbid) call the
>overloaded  void operator delete(void *)  operator.
>
>WHAT SHOULD HAPPEN IN THIS SITUATION?

Again, there is no "standard" as yet.

(Follow-ups to comp.std.c++ please.)

--

-- Ronald F. Guilmette, Sunnyvale, California -------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------




Author: kanze@us-es.sel.de (James Kanze)
Date: 30 Nov 1993 12:02:19 GMT
Raw View
In article <CH9Fq3.27u@cobra.cs.unm.edu> shapiro@cobra.cs.unm.edu
(Henry Shapiro) writes:

|> This discussion of deleting a pointer more than once brings up a related
|> question.  The "standard" says that  delete ptr  is harmless if  ptr == 0 .
|> The background to this question is:
|>   (1) ptr was declared  X* p  where  X  is a class.
|>   (2) class X overloads  delete .
|>   (3) class X has a destructor.
|> All the compilers I have easy access to when executing  delete ptr  with
|>  ptr == 0  appear to do "nothing", i.e., they certainly do not/cannot call
|> the destructor on the object to which  p  points, since it doesn't point
|> to an object.  BUT IT ALSO DOES NOT CALL THE CODE FOR THE OVERLOADED delete.
|> The "standard" does not seem to address the issue of whether it should or
|> should not (or is implementation dependent :-( heaven forbid) call the
|> overloaded  void operator delete(void *)  operator.

|> WHAT SHOULD HAPPEN IN THIS SITUATION?

Interesting question.  The standard says that deleting a null pointer
is safe, but should we take this to mean: a) 'operator delete' must
work when called with a null pointer, or b) 'operator delete' will not
be called if the pointer is null.

Of course, if we choose a) then we also have to decide *which*
'operator delete' gets called, the global one, or the one for the
static type of the pointer.  (It would be very difficult to call the
one for the dynamic type of a null pointer.)

My personal preference: since it will be necessary to generate a test
for null inline anyway if the class has a virtual destructor (which
cannot be called on a null pointer), I would favor either a), or
implementation defined.  I do not see it as a major problem, though.
All user provided 'operator delete' should check for null, and do
nothing in that case, so it really shouldn't matter which one, if any,
is called.

Followups to comp.lang.c++.
--
James Kanze                             email: kanze@us-es.sel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung