Article View: comp.std.c++
Article #1955new and delete
From: Christian E. Hopps
Date: Wed, 13 Sep 1995 03:10
Date: Wed, 13 Sep 1995 03:10
82 lines
4185 bytes
4185 bytes
I have recently been working with a compiler vendor to fix some bugs regarding
exceptions thrown during construction of "new" allocated objects.
I have become totally confused as to the appropriate behavior when
dealing with new-placement expressions. Most of the confusion
stems from a lack of clarity in the WP (this could just be me though.)
First section 5.3.4(*) mentions an operator delete() that has almost the
same type as the placement operator new() being used (all but the first
argument matching.)
Does this mean if I have e.g. void* operator new(size_t, void*)
I can have an operator delete(void*, void*)? Given the grammar
for actually using "delete" there is no way to call this operator
delete. (other than throwing an exception from a constructor :)
Well 5.3.4(*) references 3.7.3.2(**) which says that for global
operator delete only one type is valid (void operator delete(void*))
but for class members one can define two, namely:
void operator delete(void*, size_t);
void operator delete(void*);
If both are given then the two argument version is a placement
version. Otherwise the one declared is a normal version and there
is no placement (does that mean that the name lookup mentioned in
5.3.4 should fail if only "operator delete(void*, size_t) is found?)
Hmm.. so basically the only way that the "name lookup" mentioned in
5.3.4 will succeed is if I have used an operator new declared as such:
void* operator new(size_t, size_t);
Thats not very common.
The fact that clause 20 of 5.3.4 exists at all makes it hard to believe
that this is the case.
I'm basically lost jumping around the WP at this point. Can someone
clarify what kinds of operator delete's are allowed and exactly what
is supposed to occur when an object constructed from a placement-new
throws while constructing?
Thanks,
Chris.
* 5.3.4 [...]
18 If the constructor throws an exception and the new-expression
contains a new-placement, a name lookup is performed on the name
of operator delete in the scope of this new-expression. If the
lookup succeeds and exactly one of the declarations found matches
the declaration of that placement operator new, then the matching
placement operator delete shall be called (3.7.3.2).
19 A declaration of placement operator delete matches the declaration
of a placement operator new when it has the same number of parameters
and all parameter types except the first are identical disregarding
top-level cv-qualifiers.
20 If placement operator delete is called, it is passed the same
arguments as were passed to placement operator new. If the
implementation is allowed to make a copy of an argument as part
of the placement new call, it is allowed to make a copy (of the
same original value) as part of the placement delete call, or
to reuse the copy made as part of the placement new call. If the
copy is elided in one place, it need not be elided in the other.
** 3.7.3.2
1 Like allocation functions, deallocation functions can be static
class member functions or global functions.
2 Each deallocation function shall return void and its first parameter
shall be void*. For class member deallocation functions, a second
parameter of type size_t may be added. If both versions are declared
in the same class, the one-parameter form is the usual deallocation
function and the two-parameter form is used for placement delete
(5.3.4). If the second version is declared but not the first, it is
the usual deallocation function, not placement delete.
---
[ 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. ]
Technical Details
Message-ID:
<v02120d01ac7b940e834a@[164.76.64.3]>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 13 Sep 95 03:10:30 GMT Approved: fjh@cs.mu.oz.au Organization: - Newsgroups: comp.std.c++ Message-ID: <v02120d01ac7b940e834a@[164.76.64.3]> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Original-Date: Tue, 12 Sep 1995 16:30:36 -0400 From: chopps@water.emich.edu (Christian E. Hopps) Subject: new and delete