Topic: Question about operator delete


Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1999/03/21
Raw View
See the thread I started in this group on 3/12/99 titled "Matching class new
and delete".  In short I think the standard currently says that in the case
you give void operator delete(void*, size_t) would be a placement delete for
which you have not provided a matching placement new, so it would never be
used.

Michael Greenberg <mgreenberg@scr.siemens.com> wrote in message
news:36F27B78.2422367B@scr.siemens.com...
>I have several questions about the following code fragment:
>
>class aaa {
> public:
> aaa() ;
>  void* operator new(size_t) ;
>  void operator delete(void*) ;
>  void operator delete(void*, size_t) ;
>} ;
> <snip>
---
[ 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: 1999/03/23
Raw View
Michael Greenberg <mgreenberg@scr.siemens.com> writes:

>I have several questions about the following code fragment:

>class aaa {
> public:
> aaa() ;
>  void* operator new(size_t) ;
>  void operator delete(void*) ;
>  void operator delete(void*, size_t) ;
>} ;

>class bbb : public aaa {
>} ;

>void foo()
>{
>  bbb* b = new bbb;
>  delete b;
>}

>Is it legal to have both forms of operator delete in a class?

Yes, but it would be unusual to want both of them.

>If so, which one is executed during the call to delete?

Section 3.7.3.2 says that if a class has an operator delete
with exactly one parameter (which must be void*), that is the
"usual deallocation function", meaning the one that is called
by a delete-expression or in a non-placement context. If there
is no such function but there is one with exactly two parameters,
void* and size_t, then that is the usual deallocation function.

Since class aaa has an operator delete(void*), that is the
one called by a delete-expression.

However, since the functions are inherited by bbb, you might not
want to have this first form of operator delete, since it will
not receive the size of the object being deallocated. (OTOH,
you might not need the size.)

>If the call to new throws an excepton, which one is executed?

Neither one, if operator new throws an exception. An operator
delete is called only if the constructor exits via an exception.
Since the new-expression is not a placement new, the usual
function is called -- operator delete(void*) in this example.

>If aaa::operator delete(void*) is not present, and the constructor does
>a throw, is operator delete(void*, size_t) supposed to be called.

Yes, that one becomes the usual deallocation function.

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






Author: Michael Greenberg <mgreenberg@scr.siemens.com>
Date: 1999/03/20
Raw View
I have several questions about the following code fragment:

class aaa {
 public:
 aaa() ;
  void* operator new(size_t) ;
  void operator delete(void*) ;
  void operator delete(void*, size_t) ;
} ;

class bbb : public aaa {
} ;

void foo()
{
  bbb* b = new bbb;
  delete b;
}

Is it legal to have both forms of operator delete in a class?
If so, which one is executed during the call to delete?
If the call to new throws an excepton, which one is executed?
If aaa::operator delete(void*) is not present, and the constructor does
a throw, is operator delete(void*, size_t) supposed to be called.

Thanks,


--
Michael Greenberg               mailto:mgreenberg@scr.siemens.com
Siemens Corporate Research      phone: 609-734-3347
755 College Road East           fax: 609-734-6565
Princeton, NJ 08540
---
[ 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              ]