Topic: delete operator and deallocation functions


Author: Sandra Loosemore <sandra@shore.net>
Date: 1998/10/22
Raw View
In section 5.3.5, in describing the behavior of delete expression, the
standard says:

"The delete-expression will call a deallocation function (3.7.3.2)."

Since deallocation functions can be overloaded, which one, exactly, is
the delete expression supposed to call?  Does it always call the
one-parameter version?  I gather that the placement deallocation
functions are only there for cleaning up failures in allocation rather
than for being called by the delete expression, but what about the
two-parameter "usual" deallocation functions?  If there is only a
two-parameter version defined for a particular class, is that an
error, or is the compiler supposed to be smart enough to figure out
what to pass it for the second (size_t) argument?  I believe the
latter would requiring the compiler to stuff the size in the vtable or
something like that, since the static type of the delete operand does
not need to exactly match its dynamic type.

-Sandra


[ 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              ]






Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/23
Raw View
Sandra Loosemore wrote:
>
> In section 5.3.5, in describing the behavior of delete expression, the
> standard says:
>
> "The delete-expression will call a deallocation function (3.7.3.2)."
>
> Since deallocation functions can be overloaded, which one, exactly, is
> the delete expression supposed to call?  Does it always call the
> one-parameter version?

Yes. The only time it calls any other version is when an exception is
thrown by a constructor for an object in newly allocated memory. In that
case, it calls whatever operator delete is the twin of the operator new
that allocated the memory.

--

Ciao,
Paul
---
[ 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              ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/10/23
Raw View
Sandra Loosemore <sandra@shore.net> writes:


>In section 5.3.5, in describing the behavior of delete expression, the
>standard says:

>"The delete-expression will call a deallocation function (3.7.3.2)."

>Since deallocation functions can be overloaded, which one, exactly, is
>the delete expression supposed to call?

Refer to the note following the sentence you quoted, and also to
section 3.7.3.2, which explains what is meant by "usual deallocation
function".

The "placement delete" forms of operator delete are never called
as the result of a delete expression. Thus, there will be at
most one function that can be called.

A "placement delete" form is called automatically only if
a constructor exits via an exception, and construction was
part of a placement-new expression. In that case, the matching
operator delete is called. See 15.2 "Constructors and destructors".

>If there is only a
>two-parameter version defined for a particular class, is that an
>error, or is the compiler supposed to be smart enough to figure out
>what to pass it for the second (size_t) argument?  I believe the
>latter would requiring the compiler to stuff the size in the vtable or
>something like that, since the static type of the delete operand does
>not need to exactly match its dynamic type.

The compiler must arrange for the size to be passed correctly.
One way to do that is to put the size in the vtable. Another
is to have the most-derived class destructor call the appropriate
operator delete. That destructor knows which is the correct
operator delete, and it also knows the size of its own class.
(A destructor would need a special flag passed in by the compiler
to tell it whether to delete the object.)

--
Steve Clamage, stephen.clamage@sun.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              ]