Topic: operator delete


Author: "Hans A. Plast" <mst@Informatik.Uni-Bremen.DE>
Date: 1999/01/30
Raw View
Hello,

i have a question regarding destruction of dynamic objects and overloading
of the delete operator:
When i do not call the global delete operator in my overloaded one, is it
possible to call delete several times?

class A
{
  public:

    void operator delete (void* p);
};



void A::operator delete (void* p)
{
  if ('any expression')
    ::operator delete p;
  else
    'do nothing'
}


So is it possible to avoid the deletion of an object of A by not calling the
global operator?




Thanks for your help,
  Michael
---
[ 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: "Sherwood Hu" <sherwood.hu@gte.net>
Date: 1999/01/30
Raw View
Sure.

Sherwood
Hans A. Plast wrote in message
<78pq8a$on8$1@kohl.informatik.uni-bremen.de>...
>Hello,
>
>i have a question regarding destruction of dynamic objects and overloading
>of the delete operator:
>When i do not call the global delete operator in my overloaded one, is it
>possible to call delete several times?
>
>class A
>{
>  public:
>
>    void operator delete (void* p);
>};
>
>
>
>void A::operator delete (void* p)
>{
>  if ('any expression')
>    ::operator delete p;
>  else
>    'do nothing'
>}
>
>
>So is it possible to avoid the deletion of an object of A by not calling
the
>global operator?
>
>
>
>
>Thanks for your help,
>  Michael
>---
>[ 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: comeau@panix.com (Greg Comeau)
Date: 1999/02/01
Raw View
In article <78pq8a$on8$1@kohl.informatik.uni-bremen.de> "Hans A. Plast" <mst@Informatik.Uni-Bremen.DE> writes:
>i have a question regarding destruction of dynamic objects and overloading
>of the delete operator:
>When i do not call the global delete operator in my overloaded one, is it
>possible to call delete several times?

What do you mean by this if somehow different from your example below?

>class A
>{
>  public:
>
>    void operator delete (void* p);
>};
>
>
>
>void A::operator delete (void* p)
>{
>  if ('any expression')
>    ::operator delete p;
>  else
>    'do nothing'
>}
>
>
>So is it possible to avoid the deletion of an object of A by not calling the
>global operator?

Some programs consider this an efficiency strategy.
Others just call this plain old sloppy and wrong.
Your question assumes that something else had called the global new operator
in order to obtain the object in question, and that may not be the case.
It also ignores things like new'ing and deleteing class based objects.
In short, I would suggest avoiding the above, getting some good books
which discuss new and delete, memory management, resource allocation, etc,
as well as looking at alternative strategies.  Your Q is just too general
to offer alternatives at this point (and then probably would really be
appropriate for this NG).

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/02/01
Raw View
Hans A. Plast wrote:
>
> Hello,
>
> i have a question regarding destruction of dynamic objects and overloading
> of the delete operator:
> When i do not call the global delete operator in my overloaded one, is it
> possible to call delete several times?
>
> class A
> {
>   public:
>
>     void operator delete (void* p);
> };
>
> void A::operator delete (void* p)
> {
>   if ('any expression')
>     ::operator delete p;
This must read
      ::operator delete(p);

>   else
>     'do nothing'
> }
>
> So is it possible to avoid the deletion of an object of A by not calling the
> global operator?

If by "the deletion" you only mean freeing the memory, yes.
But note that your destructor will be run at the first delete,
and after that you'll have raw memory only. Assume you have
the following operator delete:

bool may_delete=true;

void A::operator delete(void* p)
{
  if (may_delete)
    ::operator delete(p);
}

Then the following function works:

void works()
{
  A* p=new A;
  A* q=p;           // delete may modify its argument!
  may_delete=false; // we don't want to release memory
  delete q;         // only destructs the object
  q=new(p) A;       // Now we construct a new object here
  may_delete=true;  // this time, we want to release memory
  delete q;         // destructs object, releases memory
}

But the following does not (even if it _may_ appear to do
on your compiler - try with inherited object and virtual
destructor printing something ...)

void bad()
{
  A* p=new A;
  A* q=p;
  may_delete=false;
  delete q;
  may_delete=true;
  delete p;         // Error: (*p) destructed twice!
}

However note that the preferred method to destruct an object
without freeing the memory is _not_ overloading operator delete,
but simply calling the destructor directly:

p->~A(); // Destruct the A p points to
---
[ 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: Jaap-Jan Boor <jjboor@lucent.com>
Date: 1998/10/26
Raw View
Hi,

I've question regarding the behaviour of operator delete.

For a particular class, I have overloaded the operator
delete to do some checking, which may result in NOT
deleting the object. I agree with you that this is not
a very beautiful design (not deleting the object while
calling the delete operator), but anyway.

The class (A1) is a derivative of another class (A) which has
not an overloaded delete operator. The behaviour I see is that
after the delete operator on the derivative A1 is called,
and the object is not deleted in this case, it is not possible
anymore to reference it's base (A). This referencing is done by
casting a pointer to the class A type when returning from some
OS call.

Is this correct behaviour? I.e. when you call an overloaded
operator delete does it do other things under water, e.g.
updating virtual tables, etc.?
Which means you can do everything you like in a overloaded
operator delete but after it returned, make sure the allocated
memory is freed and don't use the object anymore (which seems
very logical to me anyway....)

I solved my problem by the way in a different way but am curious
what delete exactly does.

Thanks for any information/answers on this,

Jaap-Jan


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