Re: placement new overhead
On Jan 29, 3:31 pm, Marc <marc.gli...@gmail.com> wrote:
I have a memory region (I got it through std::aligned_storage), and I
initialize it using placement new. Looking at the generated code
(gcc-4.6), I notice that:
new(location) Type(arguments);
is implemented as:
if(location!=0) call the constructor
this causes quite a bit of overhead when Type is a trivial type (the
compiler can't always prove that location is not null).
I can't seem to find where the standard says that placement new can be
called with a null argument and should do nothing in that case. Is
this the case?
The signature of placement new is:
void* operator new( size_t, void* ) throw();
The "throw()" means that operator new can return a null pointer,
and that the generated code must behave correctly if it does.
(That's the general rule for all operator new functions: if they
have an empty exception specifier---they declare that they never
throw---then they report insufficient memory by returning a null
pointer.) From a language point of view, this means that the
compiler must test for null before calling the constructor.
With regards to this placement form of operator new in
particular, the standard doesn't say anything about the second
argument, *except* that it will be returned immediately. So
presumably, any pointer which would be valid as a return value
of this operator new would be a valid argument. And because of
the throw(), null is a valid return value.
--
James Kanze