Topic: Placement new and delete


Author: b91926@fsgi02.fnal.gov (David Sachs)
Date: 1996/08/14
Raw View
fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) writes:

...
>Instead, you need to invoke the desstructor and the placement
>delete function explicitly; a template function can help with this.

> template <class Param1, Class T>
> void Delete(const Param &param, T* pointer) {
>  pointer->~T();
>  operator delete(p, param);
> }

> ...
> Delete(5, x);

>This sort of work-around is suggested in the ARM, which argued that it
>wasn't necessary to support special syntax for placement delete,
>because if you really placement delete, you could always use a work-around
>like the above.

Actually you probably add a little to thwe code example above. It may
be unsafe to convert the pointer to a void* after the object has been
destroyed, so the function should look like:

 template <class Param1, Class T>
 void Delete(const Param &param, T* pointer) {
  void *vp = static_cast<void *>(pointer);
  pointer->~T();
  operator delete(vp, param);
 }

I consider code like this to be ugly. I feel the standards committee
should bite the bullet, and add placement delete to the language. The
ambiguous forms, that would have to be parsed as placement deletes, are
probably rare in production code, and can be disambiguated by adding
a pair of parentheses. e.g.  The expression

delete (5) +x;   is treated as a placement delete. If a non-placement
delete is intended add parentheses to get: delete ((5) +x);
--
** 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 708 840 3942      Deparment Fax: 1 708 840 3785


[ 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: 100754.2730@compuserve.com (Martin Aupperle)
Date: 1996/08/09
Raw View
Hello!
Recently I tried to figure out how to use my own allocation and
deallocation functions.
For allocation, I want to pass additional arguments. I know how to do
this by providing e.g. an operator new( size_t, int  ) and writing

  X* x = new( 5 ) X( ... );

But now the problem: The DWP states that a placement delete can be
defined, but that it will be called when an exception in the
constructor of X occurs, and that it will be provided the same
arguments as placement new. Can I conclude from that that it is not
possible to write

  delete( 5 ) x;

(My old compiler does not allow it anyway). If it is not allowed, how
can one distinguish between a "standard" allocation and a placement
new when it comes to deletion?

Thank you - Martin




[ 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: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/08/11
Raw View
Hi,
Martin Aupperle (100754.2730@compuserve.com) wrote:
: For allocation, I want to pass additional arguments. I know how to do
: this by providing e.g. an operator new( size_t, int  ) and writing

:   X* x = new( 5 ) X( ... );

: But now the problem: The DWP states that a placement delete can be
: defined, but that it will be called when an exception in the
: constructor of X occurs, and that it will be provided the same
: arguments as placement new. Can I conclude from that that it is not
: possible to write

:   delete( 5 ) x;

Correct. The syntax for the deletion is this:

    x.~X();
    operator delete(x, 5);

The arguments passed to 'operator delete()' are provided by the
compiler only when an exception is thrown during construction of the
object (and are the same as the arguments to placement new). If no
exception is thrown, the user has to make sure that the object is
explicitly destructed and then the 'operator delete()' with appropriate
arguments has to be called to release the memory.
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic


[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/08/12
Raw View
100754.2730@compuserve.com (Martin Aupperle) writes:

>For allocation, I want to pass additional arguments. I know how to do
>this by providing e.g. an operator new( size_t, int  ) and writing
>
>  X* x = new( 5 ) X( ... );
>
>But now the problem: The DWP states that a placement delete can be
>defined, but that it will be called when an exception in the
>constructor of X occurs, and that it will be provided the same
>arguments as placement new. Can I conclude from that that it is not
>possible to write
>
>  delete( 5 ) x;

Yes, your conclusion is correct; there is no special syntax for
calling placement delete.

Instead, you need to invoke the desstructor and the placement
delete function explicitly; a template function can help with this.

 template <class Param1, Class T>
 void Delete(const Param &param, T* pointer) {
  pointer->~T();
  operator delete(p, param);
 }

 ...
 Delete(5, x);

This sort of work-around is suggested in the ARM, which argued that it
wasn't necessary to support special syntax for placement delete,
because if you really placement delete, you could always use a work-around
like the above.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/08/12
Raw View
In article <4ui8cg$2ma@news.BelWue.DE>
kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl) writes:

|> Hi,
|> Martin Aupperle (100754.2730@compuserve.com) wrote:
|> : For allocation, I want to pass additional arguments. I know how to do
|> : this by providing e.g. an operator new( size_t, int  ) and writing

|> :   X* x = new( 5 ) X( ... );

|> : But now the problem: The DWP states that a placement delete can be
|> : defined, but that it will be called when an exception in the
|> : constructor of X occurs, and that it will be provided the same
|> : arguments as placement new. Can I conclude from that that it is not
|> : possible to write

|> :   delete( 5 ) x;

|> Correct. The syntax for the deletion is this:

|>     x.~X();
|>     operator delete(x, 5);

Attention.  Invoking "operator delete" does NOT do the same thing as a
delete expression.  A delete expression first calls any destructors;
"operator delete" does not.

|> The arguments passed to 'operator delete()' are provided by the
|> compiler only when an exception is thrown during construction of the
|> object (and are the same as the arguments to placement new). If no
|> exception is thrown, the user has to make sure that the object is
|> explicitly destructed and then the 'operator delete()' with appropriate
|> arguments has to be called to release the memory.

Correct.  The first thing, however, is to know what the additional
arguments to new are doing.  Typically, I find that when I start
defining "funny" operator new's, I also have to replace the standard
operator new and operator delete, and arrange on my own for operator
delete to be able to distinguish which operator new allocated the
memory.

Note that formally, using delete (the expression) on a pointer not
returned by a non-placement new is undefined.  I presume that this
restriction 1) is not valid if you provide both the operator new and the
operator delete involved, and 2) that deleting memory allocated with the
nothrow version of new will be made defined in a future draft.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils,    tudes et r   alisations en logiciel orient    objet --
                -- A la recherche d'une activit    dans une region francophone



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