Topic: placement delete


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/12
Raw View
Stefan Rupp wrote:

> is it true that the placement delete operator (see 18.4.1.3) can't
> be called explicitly?

Yes, the following is incorrect:

  delete (foobar) p;

you have to do the work yourself:

void Delete (T* p, Foobar foobar)
{
    try {
        p->~T ();
    } catch (...) {
        operator delete (p, foobar);
        throw;
    }
    operator delete (p, foobar);
}

--

Valentin Bonnard
---
[ 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: Stefan Rupp <struppi@gia.rwth-aachen.de>
Date: 1999/09/09
Raw View
Good afternoon,

is it true that the placement delete operator (see 18.4.1.3) can't
be called explicitly?

Doei,
     struppi

--
Stefan Rupp                                    Email: struppi@acm.org
Geodaetisches Institut der RWTH Aachen         Tel.:  +49 241 80-5295
Templergraben 55, D-52062 Aachen, Germany      Fax:  +49 241 8888-142
---
[ 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: herbs@cntc.com (Herb Sutter)
Date: 1998/06/10
Raw View
Dave Moore <sponk@geocities.com> wrote:
>Samphan Raruenrom wrote:
>> Do we have the "placement delete"?  So I can have a private
>> memory allocator specific to some instances (e.g. for lock-free
>> thread-safety) like :-
>>
>> AutoHeap aheap(100);
>> Obj *obj = new(aheap) Obj;

You can have such a specific memory allocator, but not what comes next:

>> ...
>> delete(aheap) Obj;

Not like that. You can overload delete with extra parameters similarly to
the way you do with what's called "placement" new, but such an overloaded
delete is ONLY ever called in the case when the corresponding placement
new fails because of an exception (this is so that you can do
cleanup/recovery that's appropriate for that particular placement new).
There is no way to explicitly call such a "placement delete" as in your
last line above.

One way to get a similar effect is to have the Obj object remember which
way it was constructed so that you can do the appropriate cleanup.


---
Herb Sutter (mailto:herbs@cntc.com)

Current Network Technologies Corp  2695 North Sheridan Way, Suite 150
www.cntc.com www.peerdirect.com    Mississauga Ontario Canada L5K 2N6


[ 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: Samphan Raruenrom <samphan@email.ksc.net>
Date: 1998/06/02
Raw View
I've tried searching the draft standard for "placement delete" but
it is not defined.  It is mentioned somewhere to handle exception
during placement new.
Do we have the "placement delete"?  So I can have a private
memory allocator specific to some instances (e.g. for lock-free
thread-safety) like :-

AutoHeap aheap(100);
Obj *obj = new(aheap) Obj;
...
delete(aheap) Obj;
---
[ 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: Dave Moore <sponk@geocities.com>
Date: 1998/06/03
Raw View
Samphan Raruenrom wrote:

> Do we have the "placement delete"?  So I can have a private
> memory allocator specific to some instances (e.g. for lock-free
> thread-safety) like :-
>
> AutoHeap aheap(100);
> Obj *obj = new(aheap) Obj;
> ...
> delete(aheap) Obj;
> ---

As far as I can tell there is no delete() operator, however in "The C++
Programming Language" (Stroustrup, 3rd. ed.), it says that the way to
manange memory that has been allocated with a placement call is to use
explicit call of destructor functions.  So for your example you would
need to write:
    obj->~Obj();
And that should take care of it.  I must say I have not tried this yet,
however.  Hope this helps.

Dave Moore

Reply to:

dtmoore@email.unc.edu



[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/06/05
Raw View
Dave Moore wrote:
>
> Samphan Raruenrom wrote:
>
> > Do we have the "placement delete"?  So I can have a private
> > memory allocator specific to some instances (e.g. for lock-free
> > thread-safety) like :-
> >
> > AutoHeap aheap(100);
> > Obj *obj = new(aheap) Obj;
> > ...
> > delete(aheap) Obj;

> As far as I can tell there is no delete() operator,

Of course there is one

> however in "The C++
> Programming Language" (Stroustrup, 3rd. ed.), it says that the way to
> manange memory that has been allocated with a placement call is to use
> explicit call of destructor functions.  So for your example you would
> need to write:
>     obj->~Obj();

So you have to write:

try {
    p->~Obj();
}
catch (...)
{
    operator delete (p, aheap);
    throw;
}
operator delete (p, aheap);

I don't like that too much. :-(

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/06/05
Raw View
In article 62141726@geocities.com, Dave Moore <sponk@geocities.com> writes:
>Samphan Raruenrom wrote:
>
>> Do we have the "placement delete"?  So I can have a private
>> memory allocator specific to some instances (e.g. for lock-free
>> thread-safety) like :-
>>
>> AutoHeap aheap(100);
>> Obj *obj = new(aheap) Obj;
>> ...
>> delete(aheap) obj; [ typo corrected ]
>> ---
>
>As far as I can tell there is no delete() operator, however in "The C++
>Programming Language" (Stroustrup, 3rd. ed.), it says that the way to
>manange memory that has been allocated with a placement call is to use
>explicit call of destructor functions.  So for your example you would
>need to write:
>    obj->~Obj();
>And that should take care of it.  I must say I have not tried this yet,
>however.  Hope this helps.

We may have some confusion between a delete-expression, e.g.
 delete obj;
and the overloaded set of functions called "operator delete".

A delete-expression invokes the class destructor, then calls the
matching version of operator delete.

You can write a placement version of an operator delete, such as
 void operator delete(void*, AutoHeap&)
if you wish. But there is no form of a delete-expression that
can access it. That is, you cannot write
 delete (aheap) obj; // error

You can call the object destructor explicitly, then call the
operator delete explicitly:
 obj->~Obj();
 operator delete(obj, aheap);

---
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: isj@image.dk
Date: 1997/08/25
Raw View
I do not completely understand how placement works on operator delete.

The December '96 Working Paper contains a lot of references to
"placement operator delete" in 3.7.3.2, 5.3.4(.18), 15.2(.2) and
18.4.1.3(.2) and explains much about them (parameters, exceptions,
prototype etc..). But I cannot find a single example of calling an
placement operator delete.

The grammar of delete-expression in 28.4 does not allow a
"delete-placement" (neither does the grammar in "C++ prog.lang." 3rd
ed.).

This could make me believe that the placement is somehow remembered and
automagically passed to a placement operator delete. But I cannot find
anything in the draft that says that it is so. It is also more-or-less
contradicted by "C++ prog.lang." page 256, section 10.4.11 which uses
the following example:
  class Arena {
    void *alloc(...
    void free(...
  }
  void *operator new(size_t sz, Arena *a)
  {
    return a->alloc(sz);
  }

  Arena Shared;

  ...
  X *q = new(Shared) X(i);

  ...
  void destroy(X* p, Arena* a)
  {
    p->~X();
    a->free(p);
  }


Is the placement from operator new remembered so it can be passed on the
operator delete, or is there an error in the grammar in both the draft
and "c++ prog.lang" ?
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Don Griffin <dgriffin@no.spam.farallon.com>
Date: 1997/08/27
Raw View
Steve Clamage wrote:
> There is no delete-expression which will invoke a placement
> form of operator delete. You can still call the function
> explicitly. Example:
>
>         // a pair of placement operators
>         void operator delete(void*, int);
>         void* operator new(size_t, int);
>
>         T* p = new (12) T; // invoke placement new
>         ...
>         // destroy object and delete space manually
>         p->~T();
>         operator delete(p, 12);
>
> If you just wrote
>         delete p;
> it would use the normal operator delete, which presumably
> is not correct, since you took the trouble to declare a
> form to match the placment new.

If I allocate an object using placement new, it seems that I must delete
it in some way other than operator delete, since that would require
extra information.  Why does the language exlude something like this?

 delete(arena) p;

Probably because of an ambiguity between whether arena or p is being
deleted, right?  Perhaps some other syntax would be more appropriate?
Eg:

 Foo * p = new<arena> Foo (n);
 delete<arena> p;

Has this been considered?  Without a symmetric mechanism, placement new
is of little use other than constructing an object in a preallocated
space.

Don Griffin
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/08/27
Raw View
Tanveer Gani wrote:
>
> Placement delete could be used to invoke the destructor of an object without
> actually freeing any memory for an object constructed at a known address:

Not so.

>
> class X { /* ... */ };
>
>         long mem[nwords];
>         X* px = new (&mem[0]) X;
>         // use px
>         delete (&mem[0]) px; // invoke ~X() but don't actually free memory

The last line is invalid. There is no "placement delete expression".
If you want to call a placement delete operator you must call it
explicitly. Example:
 operator delete(px, &mem[0]); // assuming the function exists

If you want to destroy an object without deleting the memory it
occupies, you call the destructor explicitly. Example:
 px->~X();

--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: b91926@fsui02.fnal.gov (David Sachs)
Date: 1997/08/27
Raw View
Tanveer Gani <tanveerg@microware.com> writes:

...
>Placement delete could be used to invoke the destructor of an object without
>actually freeing any memory for an object constructed at a known address:

> delete (&mem[0]) px; // invoke ~X() but don't actually free memory

One reason the proposed c++ standard does not provide for easy use
of placement delete, is that the simplest way to do it can be
ambiguous. e.g

delete (a) *b // treated as delete ((a)*b);

Even requiring redunant parentheses does not help;

delete (a) (b)  // parsed as if a is a function - delete ((a)(b))
--
** The Klingons' favorite food was named by the first earthling to see it **
David Sachs - Fermilab, MSSG MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 630 840 3942      Department Fax: 1 630 840 3785
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: isj@image.dk
Date: 1997/08/27
Raw View
Pete Becker <petebecker@acm.org>26 Aug 1997 12:44:02 PDT writes:
:>You cannot explicitly invoke placement delete. It's there for the
:>compiler to use when a constructor, invoked through placement new,
:>throws an exception.

For the sake of symmetry I would like that I could invoke placement delete
myself. Eg. instead of having to call the destructor directly and then calling
the free-memory function directly:
  Arena Shared;
  //allocate memory and construct object
  X *p = new(Shared) X(i);
  //destroy object and release memory
  p->~X();
  Shared->free(p);

I would like that I could call placement delete, eg:
  Arena Shared;
  ...
  X *p = new(Shared) X(i);
  ...
  delete(Shared) p;


Does anyone have an idea why calling placement delete is not allowed? (except
that there is no grammar for it)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Christian Millour <chris161@club-internet.fr>
Date: 1997/03/27
Raw View
Hi.

CD2 is conspicuously devoid of examples of use of placement delete.
My compiler doesn't currently support them but I'm curious. Are the
following snippets well formed and do they behave as expected ?

here I would expect a call to ~derived() :

void
test1(void * raw) {
  base * bp = new (raw) derived;
  // ...
  delete (raw) bp; // SYNTAX ???
}

and here I would expect `count' calls to ~whatever(), in reverse
order of construction.

void
test2(int count, void * raw) {
  whatever * wp = new (raw) whatever[count];
  ...
  delete[] (raw) wp; // SYNTAX ???
}

TIA
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/03/28
Raw View
Christian Millour <chris161@club-internet.fr> wrote:
: Hi.

: CD2 is conspicuously devoid of examples of use of placement delete.

Placement delete is not meant for a programmer to deallocate memory,
"alocated" by placement new. The corresponding placement delete is called
by the compiler if the correspoding placement new throws an exception.

: My compiler doesn't currently support them but I'm curious. Are the
: following snippets well formed and do they behave as expected ?

: here I would expect a call to ~derived() :

: void
: test1(void * raw) {
:   base * bp = new (raw) derived;
:   // ...
:   delete (raw) bp; // SYNTAX ???
: }

What you probably have in mind, is to destruct the object pointed to by bp,
not to deallocate the memory it occupies.
If so, use bp->~base() (your destructor is virtual, right?), or
bp->~derived().

If you indeed want to deallocate memory pointed to by raw, use
delete raw, or free(raw), or whatever is appropriate.

: and here I would expect `count' calls to ~whatever(), in reverse
: order of construction.

: void
: test2(int count, void * raw) {
:   whatever * wp = new (raw) whatever[count];
:   ...
:   delete[] (raw) wp; // SYNTAX ???
: }

Same as above.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
 ==> http://www.math.psu.edu/zabluda/sheep.html before e-mailing about cloning.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/03/28
Raw View
Oleg Zabluda <zabluda@math.psu.edu> wrote:
: Placement delete is not meant for a programmer to deallocate memory,
: "alocated" by placement new. The corresponding placement delete is called
: by the compiler if the correspoding placement new throws an exception.

Correcting myself: not when placement new throws an exception, but when
a constructor throws an exception during evaluation of the "placement
new expression".

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: herbs@cntc.com (Herb Sutter)
Date: 1997/03/29
Raw View
Christian Millour <chris161@club-internet.fr> wrote:
>CD2 is conspicuously devoid of examples of use of placement delete.
>My compiler doesn't currently support them but I'm curious. Are the
>following snippets well formed and do they behave as expected ?

Nope.

>here I would expect a call to ~derived() :
>
>void
>test1(void * raw) {
>  base * bp = new (raw) derived;
>  // ...
>  delete (raw) bp; // SYNTAX ???

The syntax is: "bp->~base();".  Placement delete doesn't do what you're
thinking.  It's not the deallocation partner for placement new... it's a
function that gets called to clean things up if there is an exception thrown
during construction for a corresponding placement new.  It's not something
that comes up very often.



Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/10/01
Raw View
rmartin@rcmcon.com (Robert Martin) writes:

>It is my understanding from the WP that placement versions of operator
>delete are only called when a constructor of a object created by the
>corresponding placment new throws an exception.  I have seen this
>confirmed by a recent thread in this newsgroup.
>
>That thread also suggested that the only way to explicitly call
>placement delete would be to overtly call the appropriate operator
>delete.  e.g.
>
> operator delete (object, arg1, arg2);
>
>Now, as I understand it, such a call will not cause the destructor of
>the object to be called.  Thus, of one is to invoke the placement
>syntax of operator delete, one must first explicitly call the
>destructor.  e.g.
>
> object->X::~X(); // destruct
>        operator delete(object, arg1, arg2); // deallocate
>
>If, in a certain application, placement delete *must* be conjugated
>with placement new, then all objects must be separately destructed and
>deleted as above; there is no single statement equivalent to
>'delete(arg1, arg2) object' that will coordinate the destruction and
>deletion.
>
>Is this correct?

Yes, this is all correct.

>If so, does the committee feel that this represents
>a weakness in the language [...]

Yes.  I believe the main reason that placement delete expressions were
not allowed is because of the syntactic ambiguities that would result
if the obvious syntax were adopted.

However, the weakness is not too great; you can achieve most of the
effect with a family of templates:

 template <class arg1, class object>
 void Delete1(arg1, object);

 template <class arg1, class arg2, class object>
 void Delete2(arg1, arg2, object)
 ...

>[...] that will be addressed in the standard?

No, at least not in this standard.  (It might conceivably be addressed
in the next version of the standard.)

--
Fergus Henderson             |  "Australia is the richest country in the world,
fjh@cs.mu.oz.au              |   according to a new system of measuring wealth
http://www.cs.mu.oz.au/~fjh  |   announced by the World Bank yesterday."
PGP: finger fjh@128.250.37.3 |  - Melbourne newspaper "The Age", 18 Sept 1995.


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: rmartin@rcmcon.com (Robert Martin)
Date: 1995/09/18
Raw View
Question:

It is my understanding from the WP that placement versions of operator
delete are only called when a constructor of a object created by the
corresponding placment new throws an exception.  I have seen this
confirmed by a recent thread in this newsgroup.

That thread also suggested that the only way to explicitly call
placement delete would be to overtly call the appropriate operator
delete.  e.g.

 operator delete (object, arg1, arg2);

Now, as I understand it, such a call will not cause the destructor of
the object to be called.  Thus, of one is to invoke the placement
syntax of operator delete, one must first explicitly call the
destructor.  e.g.

 object->X::~X(); // destruct
        operator delete(object, arg1, arg2); // deallocate

If, in a certain application, placement delete *must* be conjugated
with placement new, then all objects must be separately destructed and
deleted as above; there is no single statement equivalent to
'delete(arg1, arg2) object' that will coordinate the destruction and
deletion.

Is this correct?  If so, does the committee feel that this represents
a weakness in the language that will be addressed in the standard?
--
Robert Martin       | Design Consulting   | Training courses offered:
Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
2080 Cranbrook Rd.  | Tel: (708) 918-1004 |   Mgt. Overview of OOT
Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: fenster@cs.columbia.edu (Sam Fenster)
Date: 1995/09/19
Raw View
rmartin@rcmcon.com (Robert Martin) writes:
> That thread also suggested that the only way to explicitly call placement
> delete would be to overtly call the appropriate operator delete.  e.g.
>
>  operator delete (object, arg1, arg2);

In fact, wouldn't you have to call `operator delete' with the address of the
most derived subclass object?  How would you do that?

> Now, as I understand it, such a call will not cause the destructor of the
> object to be called.  Thus, of one is to invoke the placement syntax of
> operator delete, one must first explicitly call the destructor.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: bgibbons@taligent.com (Bill Gibbons)
Date: 1995/09/20
Raw View
In article <FENSTER.95Sep18204129@shadow.cs.columbia.edu>,
fenster@cs.columbia.edu (Sam Fenster) wrote:
> rmartin@rcmcon.com (Robert Martin) writes:
> > That thread also suggested that the only way to explicitly call placement
> > delete would be to overtly call the appropriate operator delete.  e.g.
> >
> >       operator delete (object, arg1, arg2);
>
> In fact, wouldn't you have to call `operator delete' with the address of the
> most derived subclass object?  How would you do that?

     operator delete (dynamic_cast<void*>(object), arg1, arg2);

--
Bill Gibbons
bgibbons@taligent.com


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1995/09/22
Raw View
kanze (kanze@lts.sel.alcatel.de) wrote:

|> The other case is managing different pools of memory, perhaps using
|> different strategies.  In this case, as far as I can see, the user
|> must also provide a non-placement form for new and delete; the normal
|> forms will *not* work.  It is up to the delete to somehow figure out
|> which strategy to use to free the memory, given only its address.

Since posting the above, an idea on how to solve this using mixins has
occurred to me.

Consider the following:

 class Pool1
 {
 public :
  void*    operator new( size_t ) ;
  void    operator delete( void* ) ;
  virtual    ~Pool1() {}
 } ;

 // Other pools the same.

 class Buffer { /* ... */ } ;

 class BufferInPool1 : public Buffer
      , public Pool1
 {
  // Eventually, constructors, since they are not inherited
 } ;

 Buffer*    p1 = new Buffer ;
 Buffer*    p2 = new BufferInPool1 ;
 //  ...
 delete p1 ;       // should use global delete
 delete p2 ;       // uses delete for Pool1

I believe that the above is legal, and will work as expected if Buffer
has a virtual destructor.  It also offers a portable solution to
determining which pool is used in the destructor, and allows defining
operator new for specific pools without also having to define the normal
operator new.

The only problem I see is that I suspect that the most frequent use of
distinct pools is shared memory, and in general, virtual functions don't
work in shared memory:-(.  (Since this is comp.STD.c++, however, and the
standard says nothing about shared memory, we can ignore this problem
here:-).)
--
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: afk@ElSegundoCA.ATTGIS.COM (Art Kaufmann)
Date: 1995/07/19
Raw View
The 28April95 CD, Clause 5.3.4 para 20 states:
  "If placement operator delete is called, ..."

This is the only mention in the CD of placement operator delete.  Is
this something new, or something that is leaving?

---
Art Kaufmann                |
afk@ElSegundoCA.ATTGIS.COM  |
--------------------------------------------------------------------------------
"... and should anyone actually read this drivel, any knowlege of your actions
will be denied by AT&T Global Information Solutions"






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/20
Raw View
In article HBu@lcpd2.SanDiegoCA.ATTGIS.COM, afk@ElSegundoCA.ATTGIS.COM (Art Kaufmann) writes:
>The 28April95 CD, Clause 5.3.4 para 20 states:
>  "If placement operator delete is called, ..."
>
>This is the only mention in the CD of placement operator delete.  Is
>this something new, or something that is leaving?

It's new. It fills a gap in the handling of allocated memory when a
constructor exits via an exception. If a placement-new was used
to get the memory, an associated placement-delete can now be
invoked to deallocate it.

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: David Byrden <100101.2547@CompuServe.COM>
Date: 1995/06/18
Raw View
>    char a[sizeof(X)];
>    X *p = new(a) X;
>
>    // ...
>
>    delete p;
>

Er... hellOOOOOOO..you're not supposed to DO this...
.in the last line, the memory manager is being asked to free
some memory which it *never owned in the FIRST place*... it is
liable to crash....

.to destroy an object created by the placement new, you have to
call the destructor *without* asking the memory manager to free
the memory. This is the call;

 p->X::~X() ;





Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/06/12
Raw View
In article <3qhonf$j9s@ra.ibr.cs.tu-bs.de>,
Andreas Rossberg <ti953017@rzcipa03.rz.tu-bs.de> wrote:
>The draft defines a special placement delete function. It's not clear to
>me how a compiler could ever ensure that code like
>
>    char a[sizeof(X)];
>    X *p = new(a) X;
>
>    // ...
>
>    delete p;
>
>calls the placement delete.
>
>Please, could somebody enlighten me?

 Sure: it only calls it when the new expression fails.
The user cannot call it with a delete expression.  (It can
be called as a function though).

 The purpose of the placement delete is to fix a hole
in the language in which the compiler is required to deallocate
storage allocate by a new expression if construction fails --
but can't because it doesn't know how.

 Now it knows how -- call the corresponding delete.


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189





Author: david@atl-intl.com
Date: 1995/05/31
Raw View
One way that occurs to me (not having read the original doc, just going on the
post) would be to keep a table of every placement new that is active,
checking the deleted pointer against entries in the table, but that seems
like a kluge, so maybe there is a better way within the constraints posted.

   David E. Miller
   Consultant
   david@atl-intl.com

In article <3qhonf$j9s@ra.ibr.cs.tu-bs.de> ti953017@rzcipa03.rz.tu-bs.de
(Andreas Rossberg) writes:

>The draft defines a special placement delete function. It's not clear to
>me how a compiler could ever ensure that code like

>    char a[sizeof(X)];
>    X *p = new(a) X;

>    // ...

>    delete p;

>calls the placement delete.

>Please, could somebody enlighten me?






Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 1995/06/01
Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:

...

>You cannot cause a placement "operator delete" to be called by using a
>delete-expression. It it called automatically only if the matching
>placement "operator new" throws an exception.

I have a sneaking suspicion, that at some point, the plan was to
allow placement forms of delete expressions, but that this never
made it to the current working paper.





Author: stidev@gate.net (Solution Technology)
Date: 1995/06/01
Raw View
Andreas Rossberg (ti953017@rzcipa03.rz.tu-bs.de) wrote:
: The draft defines a special placement delete function. It's not clear to
: me how a compiler could ever ensure that code like

:     char a[sizeof(X)];
:     X *p = new(a) X;

:     // ...

:     delete p;

Its done by:       p->~X()
which dies not deallocate "a" but destructs the "X".

Ken Walter






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/01
Raw View
In article j9s@ra.ibr.cs.tu-bs.de, ti953017@rzcipa03.rz.tu-bs.de (Andreas Rossberg) writes:
>The draft defines a special placement delete function. It's not clear to
>me how a compiler could ever ensure that code like
>
>    char a[sizeof(X)];
>    X *p = new(a) X;
>
>    // ...
>
>    delete p;
>
>calls the placement delete.

No, it doesn't.

You cannot cause a placement "operator delete" to be called by using a
delete-expression. It it called automatically only if the matching
placement "operator new" throws an exception.

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: ti953017@rzcipa03.rz.tu-bs.de (Andreas Rossberg)
Date: 1995/05/31
Raw View
The draft defines a special placement delete function. It's not clear to
me how a compiler could ever ensure that code like

    char a[sizeof(X)];
    X *p = new(a) X;

    // ...

    delete p;

calls the placement delete.

Please, could somebody enlighten me?


 - Andreas Rossberg