Topic: placement new: deadlock?
Author: SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)")
Date: Thu, 22 Jul 2004 02:22:49 GMT Raw View
"David Abrahams" <dave@boost-consulting.com> wrote in message
news:u3c3nynjj.fsf@boost-consulting.com...
> SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website
for Email)") writes:
>
> > Section 18.4.1.3 of the standard takes as few prisoners as it gets:
"These
> > functions are reserved, a C++ program may not define functions that
displace
> > the versions in the Standard C++ library".
> >
> > However, if a class defines an overload of new, it also must define
> > overloads of the nothrow and placement new. Otherwise, code trying to
use
> > those two will fail to find the name (overloading doesn't work across
> > scopes).
> >
> > In short: (1) the standard says you can't "displace" the standard
placement
> > new; (2) you must implement when you define a class-specific new.
> >
> > Deadlock!!!
>
> Nasty! This looks like a core language issue to me.
As Christoph Schulz clarified, the standard mentions that you shouldn't mess
with the *global* placement new.
It's unclear to me whether the standard specifies that the standard library
must call "new(p) A"; or "::new(p) A". In the former case, I think the
standard needs to specify that any class-specific overload of the placement
new must return the passed-in pointer.
Herb tested vector<A> with a class A that defines some overload of new. That
would mask all new's within A. MSVC, gcc, and comeau fail to compile the
code because they fail to find the placement new. They must be calling
"new(p) A". Metrowerks 8 did compile the code. I don't have MWCW 8 (poor
grad student y'know) but presumably they call "::new(p) A". I find that a
nice touch.
Looks like Howard is ahead of the pack again. His STL is almost as good as
yasli. :o)
Andrei
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)")
Date: Thu, 22 Jul 2004 02:26:30 GMT Raw View
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:40fcc1db.458770468@news.individual.net...
> * "Andrei Alexandrescu (See Website for Email)":
> > Section 18.4.1.3 of the standard takes as few prisoners as it gets:
"These
> > functions are reserved, a C++ program may not define functions that
displace
> > the versions in the Standard C++ library".
> >
> > However, if a class defines an overload of new, it also must define
> > overloads of the nothrow and placement new. Otherwise, code trying to
use
> > those two will fail to find the name (overloading doesn't work across
> > scopes).
>
> Uhm, there are infinitely many signatures for placement new...
Yes.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&threadm=2lrofeFfvkiaU1%40uni-berlin.de&rnum=1&prev=/groups%3Fq%3Dgroup:comp.lang.c%252B%252B.moderated%2Binsubject:new%2Bauthor:andrei%2Bauthor:alexandrescu%26hl%3Den%26lr%3D%26ie%3DUTF-8%26safe%3Doff%26selm%3D2lrofeFfvkiaU1%2540uni-berlin.de%26rnum%3D1
> 12.5/2 has an example defining
>
> class Arena;
> struct B{ void* operator new( size_t, Arena* ); };
>
> Also 5.3.4/10 states that
>
> The new-placement syntax is used to supply additional arguments to an
> allocation function. If used, overload resolution is performed on a
> function call created by assembling an argument list...
>
> So [these two examples mean that] presumably the restriction on not
> displacing placement new is only for the void* form, and perhaps only
> for the global one -- I think the standard is far too vague, here.
It's just a slightly confusing (albeit consistent) terminology. "A placement
allocation/deallocation function" is any overload of new/delete that takes
extra parameters, whereas "the [standard] library's placement
allocation/dellocation function", also informally referred to as "THE
placement new", is the one and only "void* operator new(std::size_t, void*
p) { return p; }" and its one and only accompanying operator delete. I
recall Scott Meyers has a sidebar in one of his articles in which he
discusses that terminology.
> > In short: (1) the standard says you can't "displace" the standard
placement
> > new; (2) you must implement when you define a class-specific new.
>
> (1) the standard operator new(size_t, void*), yes.
> (2) no.
>
> If client code wishes to use the "identity" placement new for a class
> that defines its own allocation function(s), then the client code must
> simply qualify the call to operator new, like so:
>
>
> p = ::new( &anArena ) B; // The global "identity" placement new.
It turns out that 3 out of 4 standard library implementations don't do that;
they just use the unqualified new, which gets them in trouble if B doesn't
define an overload. I think it's here where the standard is unclear.
This brings me to a quite pernicious potential bug. Say someone defines:
class A {
...
void* operator new(std::size_t, void* p) { return p; } // for political
correctness
void* operator new(std::size_t, LightSpeedAllocator& a) { ... }
};
Now if a user forgets to pass a reference and passes a pointer instead, all
the hell breaks loose:
// I want to use the special allocator
LightSpeedAllocator lsa;
A* p = new(&lsa) A;
The code above plans on calling the special overload, but added a spurious
"&" (Freudian slip from my C days, your honor!) and youza, the address is
converted to void* and presto passed to the placement new, which simply
passes back lsa's address!
I think there are some measures to be taken in the standard. The simplest
would be to guarantee that the standard library always calls the global
placement new operator.
As things stand now, it is undefined whether std::vector<A> compiles or not
if A defines whatever innocent overload of operator new. Time for a defect
report?
Andrei
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)")
Date: Tue, 20 Jul 2004 06:02:35 GMT Raw View
Section 18.4.1.3 of the standard takes as few prisoners as it gets: "These
functions are reserved, a C++ program may not define functions that displace
the versions in the Standard C++ library".
However, if a class defines an overload of new, it also must define
overloads of the nothrow and placement new. Otherwise, code trying to use
those two will fail to find the name (overloading doesn't work across
scopes).
In short: (1) the standard says you can't "displace" the standard placement
new; (2) you must implement when you define a class-specific new.
Deadlock!!!
Andrei
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)")
Date: Tue, 20 Jul 2004 17:52:13 GMT Raw View
""Andrei Alexandrescu (See Website for Email)""
<SeeWebsiteForEmail@moderncppdesign.com> wrote in message
news:2m2h9dFidvvdU1@uni-berlin.de...
> Section 18.4.1.3 of the standard takes as few prisoners as it gets: "These
> functions are reserved, a C++ program may not define functions that
displace
> the versions in the Standard C++ library".
>
> However, if a class defines an overload of new, it also must define
> overloads of the nothrow and placement new. Otherwise, code trying to use
> those two will fail to find the name (overloading doesn't work across
> scopes).
References to back this up: Exceptional C++ Style by Herb Sutter, Items 22
and 23.
> In short: (1) the standard says you can't "displace" the standard
placement
> new; (2) you must implement when you define a class-specific new.
I meant "(2) you must implement the standard placement new within the class'
scope when you define any class-specific new".
> Deadlock!!!
I meant: Deadlock!!!
I guess it all depends on what "displace" means. If it means "reimplement by
user code with different semantics" that's cool. If it means "reimplement by
user" then maybe a defect report would be in order?
:o)
Andrei
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kristov@arcor.de (Christoph Schulz)
Date: Tue, 20 Jul 2004 17:52:12 GMT Raw View
Hallo!
Andrei Alexandrescu wrote:
> Section 18.4.1.3 of the standard takes as few prisoners as it gets:
> "These functions are reserved, a C++ program may not define functions
> that displace the versions in the Standard C++ library".
>
> However, if a class defines an overload of new, it also must define
> overloads of the nothrow and placement new. Otherwise, code trying to
> use those two will fail to find the name (overloading doesn't work
> across scopes).
>
> In short: (1) the standard says you can't "displace" the standard
> placement new; (2) you must implement when you define a
> class-specific new.
I think what is meant is that you aren't allowed to replace the global
versions of placement new/delete. The paragraph you cited refers to the
declarations found in the <new> header (18.4), and these are the global
ones. In my opinion, nothing disallows defining placement new/delete
operators within class scope.
Regards,
Christoph
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: alfps@start.no (Alf P. Steinbach)
Date: Wed, 21 Jul 2004 14:28:01 GMT Raw View
* "Andrei Alexandrescu (See Website for Email)":
> Section 18.4.1.3 of the standard takes as few prisoners as it gets: "These
> functions are reserved, a C++ program may not define functions that displace
> the versions in the Standard C++ library".
>
> However, if a class defines an overload of new, it also must define
> overloads of the nothrow and placement new. Otherwise, code trying to use
> those two will fail to find the name (overloading doesn't work across
> scopes).
Uhm, there are infinitely many signatures for placement new...
12.5/2 has an example defining
class Arena;
struct B{ void* operator new( size_t, Arena* ); };
Also 5.3.4/10 states that
The new-placement syntax is used to supply additional arguments to an
allocation function. If used, overload resolution is performed on a
function call created by assembling an argument list...
So [these two examples mean that] presumably the restriction on not
displacing placement new is only for the void* form, and perhaps only
for the global one -- I think the standard is far too vague, here.
> In short: (1) the standard says you can't "displace" the standard placement
> new; (2) you must implement when you define a class-specific new.
(1) the standard operator new(size_t, void*), yes.
(2) no.
If client code wishes to use the "identity" placement new for a class
that defines its own allocation function(s), then the client code must
simply qualify the call to operator new, like so:
p = ::new( &anArena ) B; // The global "identity" placement new.
The effect on successful construction is then guaranteed, namely
(p == &anArena).
Otherwise the call might invoke B::operator new.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 21 Jul 2004 14:28:08 GMT Raw View
SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)") writes:
> Section 18.4.1.3 of the standard takes as few prisoners as it gets: "These
> functions are reserved, a C++ program may not define functions that displace
> the versions in the Standard C++ library".
>
> However, if a class defines an overload of new, it also must define
> overloads of the nothrow and placement new. Otherwise, code trying to use
> those two will fail to find the name (overloading doesn't work across
> scopes).
>
> In short: (1) the standard says you can't "displace" the standard placement
> new; (2) you must implement when you define a class-specific new.
>
> Deadlock!!!
Nasty! This looks like a core language issue to me.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: SeeWebsiteForEmail@moderncppdesign.com ("Andrei Alexandrescu (See Website for Email)")
Date: Wed, 21 Jul 2004 14:30:21 GMT Raw View
"Christoph Schulz" <kristov@arcor.de> wrote in message
news:2m3uifFii04rU1@uni-berlin.de...
> Hallo!
>
> Andrei Alexandrescu wrote:
> > Section 18.4.1.3 of the standard takes as few prisoners as it gets:
> > "These functions are reserved, a C++ program may not define functions
> > that displace the versions in the Standard C++ library".
> >
> > However, if a class defines an overload of new, it also must define
> > overloads of the nothrow and placement new. Otherwise, code trying to
> > use those two will fail to find the name (overloading doesn't work
> > across scopes).
> >
> > In short: (1) the standard says you can't "displace" the standard
> > placement new; (2) you must implement when you define a
> > class-specific new.
>
> I think what is meant is that you aren't allowed to replace the global
> versions of placement new/delete. The paragraph you cited refers to the
> declarations found in the <new> header (18.4), and these are the global
> ones. In my opinion, nothing disallows defining placement new/delete
> operators within class scope.
I think you're right. Whew. :o) Thanks!
Andrei
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]