Placement new in std::aligned_storage? - c++

Suppose I have a type template parameter T.
And suppose I have a std::aligned_storage as follows:
typename std::aligned_storage<sizeof(T), alignof(T)>::type storage;
I want to placement new a T into the storage.
What is the standard-compliant pointer value/type to pass to the placement new operator, and how do I derive that from storage?
new (& ???) T(a,b,c);
For example:
new (&storage) T(a,b,c);
new (static_cast<void*>(&storage)) T(a,b,c);
new (reinterpret_cast<T*>(&storage)) T(a,b,c);
new (static_cast<T*>(static_cast<void*>(&storage));
Which of the above (if any) are compliant, and if none, what is the better way?

The most paranoid way is
::new ((void *)::std::addressof(storage)) T(a, b, c);
Explanation:
::std::addressof guards against overloaded unary operator& on storage, which is technically allowed by the standard. (Though no sane implementation would do it.) The ::std guards against any non-top-level namespaces (or classes) called std that might be in scope.
(void *) (which in this case is the equivalent of a static_cast) ensures that you call the placement operator new taking a void * rather than something else like decltype(storage) *.
::new skips any class-specific placement operator news, ensuring that the call goes to the global one.
Together, this guarantees that the call goes to the library placement operator new taking a void *, and that the T is constructed at where storage is.
In most sane programs, though,
new (&storage) T(a,b,c);
should be sufficient.

The placement allocation function is described as follows (C++14 n4140 18.6.1.3):
void* operator new(std::size_t size, void* ptr) noexcept;
Returns: ptr.
Remarks: Intentionally performs no other action.
20.10.7.6 table 57 describes aligned_storage<Len, Align> thus:
The member typedef type
shall be a POD type suitable for use
as uninitialized storage for any object
whose size is at most Len and whose
alignment is a divisor of Align.
This implies that in your case, &storage is suitably aligned for holding an object of type T. Therefore, under normal circumstances1, all 4 ways you've listed of calling placement new are valid and equivalent. I would use the first one (new (&storage)) for brevity.
1 T.C. correctly pointed out in the comments that it is technically possible for your program to declare an overload of the allocation function taking a typename std::aligned_storage<sizeof(T), alignof(T)>::type*, which would then be selected by overload resolution instead of the library-provided 'placement new' version.
I would say this unlikely in at least 99.999% of cases, but if you need to guard against that as well, use one of the casts to void*. The direct static_cast<void*>(&storage) is enough.
Also, if you're paranoid to this level, you should probably use ::new instead of just new to bypass any class-specific allocation functions.

Related

Question about new placement related to class constructor

As far as I know the new keyword performs the process of allocating memory and call constructor of object.
class X{
public:
int x;
X(int a):x(a){std::cout<<"X(int a)"<<std::endl;}
~X(){std::cout<<"Delete X"<<std::endl;}
};
int main()
{
X* ptr = new X{2};
// allocate mem sizeof(X);
// call constructor of X{2};
delete(ptr);
}
Also new operator can be modified like below code.
X* ptr = static_cast<X*>( operator new(sizeof(X)) );
new(ptr) X{2};
My question is how to allocate constructor to ptr?
The "new(ptr) X{2}" is implemented like this.
It looks like there is no code that associated with class constructors.
How does this call a constructor?
new expressions and operator new are not the same thing. Unfortunately, they have names that suggest that operator new is like e.g. an operator overload operator+ for the + operator, which is however not the case.
A new expression may call an operator new overload, but that is only for the allocation step you are talking about, which is why the standard library placement-new operator new implementation is just a noop.
The construction of the object, including the constructor call if any is to be done, is an intrinsic part of the semantics of the new expression itself that can't be modified. There is no function implementing it and there wouldn't (generally) be any way to implement the construction of an object other than using a new expression itself.
Also, note that your replacement for the allocating new expression is not correct with your example X. X has a non-trivial destructor and is therefore not an implicit-lifetime type. This means that an X object is created only by the placement-new expression in the second line. The result of the operator new call (and also the result of the static_cast) will not be pointing to any X object.
It should be
void* mem = operator new(sizeof(X));
X* ptr = new(mem) X{2};
instead, so that ptr is guaranteed to point to the newly created object.
In general, you also need to make sure that the pointer returned from operator new is suitably aligned for the type X. Otherwise, the placement-new expression will not work correctly.
Since C++17 there is the __STDCPP_DEFAULT_NEW_ALIGNMENT__ macro against which you can test alignment to verify that the standard library's global replaceable operator new implementations without std::align_val_t parameter will guarantee suitable alignment:
static_assert(alignof(X) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__);
If that is not satisfied you must use the std::align_val_t overload:
void* mem = operator new(sizeof(X), std::align_val_t{alignof(X)});
Also, note that you should then (and only then) call the corresponding operator delete with the alignment to deallocate the memory (otherwise without the alignment argument):
ptr->~X();
operator delete(mem, std::align_val_t{alignof(X)});
The allocating new X{2} and delete(ptr); expressions do all of this decision-making on which operator new and operator delete to use internally, based on knowing the type of the operand.
(The above alignment consideration applies to the global replaceable operator new/operator delete overloads. A user-replacement of these operator new overloads must also satisfy the same requirements. However, a custom overload (rather than a replacement) may have other behavior and might be called from these expressions as a result of overload resolution.)

Why does placement-new call the standard placement operator new function?

A placement-new expression that takes a single argument that is a pointer to pre-allocated memory will construct an object of type T in that memory.
Why does it call the standard placement-new operator void* operator new ( std::size_t count, void* ptr ); since the latter does nothing and just returns its pointer argument?
int x = 10;
int* p = new(&x) int{1024};
Could you explain the steps taken in the above by the compiler to construct a new int in the memory address of x?
Why doesn't the placement-new expression directly construct an object at the memory address it gets as a pointer, rather than calling an operator function that does nothing and just returns its pointer argument?
The general rule is that new (args...) T will call operator new(sizeof(T), args...), and this function is required to return void*. If this operator new call returns successfully, the object is then constructed into the memory pointed to by the return value.
This general rule is powerful enough to support both the ordinary new expression new int and the placement form new (&x) int without any special cases. These two expressions call different overloads of operator new, which is why the former allocates and the latter does not. No matter what, an object is constructed at the end (unless the operator new function failed by throwing an exception).
There is no need to have a special rule in the language that says operator new is not called by a placement new expression. Instead, the compiler can simply optimize the code by directly constructing the int object into &x without calling operator new first, since it already knows that the placement operator new will just return its second argument.
(Actually, the reality is a bit more complicated than this. If T is an array type, then operator new[] is called instead of operator new, and the compiler may request from operator new[] a greater amount of memory than the array will actually occupy, and adjust the returned pointer before constructing the array. There are also special rules relating to over-aligned types, and there actually is some special-casing for placement new and delete. These details are not relevant to this answer.)

Using placement new in generic programming

When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitialized_copy: ([uninitialized.copy])
template <class It, class For>
For uninitialized_copy(It first, It last, For dest)
{
using T = typename std::iterator_traits<For>::value_type;
for (; first != last; ++first, (void)++dest)
::new (static_cast<void*>(std::addressof(*dest))) T(*first);
}
This post addresses the following points from the perspective of the standard:
why ::new is used instead of just new;
why an explicit cast to void* is required.
(This answer uses N4659, the final C++17 draft.)
Why ::new is used instead of just new
::new ensures that the operator new is looked up in the global scope. In contrast, the plain new first looks up in the scope of the class if T is a class type (or array thereof), and only then falls back to the global scope. Per [expr.new]/9:
If the new-expression begins with a unary ​::​ operator, the
allocation function's name is looked up in the global scope.
Otherwise, if the allocated type is a class type T or array thereof,
the allocation function's name is looked up in the scope of T. If
this lookup fails to find the name, or if the allocated type is not a
class type, the allocation function's name is looked up in the global
scope.
For example, with
struct C {
void* operator new(std::size_t, void* ptr) noexcept
{
std::cout << "Hello placement new!\n";
return ptr;
}
};
The plain new will cause this function to be found, thus printing unwanted message, whereas ::new will still find the global function and work properly.
The global operator new(std::size_t, void*) cannot be replaced because of [new.delete.placement]/1:
These functions are reserved; a C++ program may not define functions that displace the versions in the C++ standard library
([constraints]). The provisions of [basic.stc.dynamic] do not apply
to these reserved placement forms of operator new and operator
delete.
(See How should I write ISO C++ Standard conformant custom new and delete operators? for more about overloading operator new.)
Why an explicit cast to void* is required
Although the global operator new(std::size_t, void*) may not be replaced, new versions of ::operator new can be defined. For example, suppose that the following declaration is placed in the global scope:
void* operator new(std::size_t, int* ptr) noexcept
{
std::cout << "Hello placement new!\n";
return ptr;
}
Then ::new(ptr) T will use this version instead of the global version, where ptr is a int* value. The pointer is explicitly cast to void* to ensure that the void* version of operator new (which we intend to call) wins in overload resolution.
From comment:
But why do we want to call exactly global new for void* if some
type has special overload of new for itself? Seems like normal
overloaded operator is more suitable - why it's not?
Normally, new is used for allocation purposes. Allocation is something the user should have control over. The user can roll out more suitable versions for a normal new.
In this case, however, we don't want to allocate anything — all we want to do is create an object! The placement new is more of a "hack" — its presence is largely due to the lack of syntax available for constructing an object at a specified address. We don't want the user to be able to customize anything. The language itself, however, doesn't care about this hack, though — we have to treat it specially. Of course, if we have something like construct_at (which is coming in C++20), we will use it!
Also note that std::uninitialized_copy is intended for the simplest case where you just want to copy construct a sequence of objects in raw allocated space. The standard containers allow you to customize not only how the elements are allocated, but also how they are constructed, by means of an allocator. Therefore, they do not generally use std::uninitialized_copy for their elements — they call std::allocator_traits<Allocator>::construct. This feature is used by std::scoped_allocator_adaptor.

Placement new and alignment in C++

Consider the following code snippet constructing an instance of a POD (plain old data) struct in-place:
#include <new>
#include <cassert>
#include <cstddef>
struct Test
{
int a;
char b;
double c;
};
int main()
{
const std::size_t minimumNumberOfBytes = sizeof( Test ) * 4;
// Get a block of memory that can accommodate a Test instance and then some!
void* const ptrToMemBlock = new char[ minimumNumberOfBytes ];
assert( ptrToMemBlock );
// Construct a Test instance in-place.
const Test* const testInstance( ::new ( ptrToMemBlock ) Test() );
// Is this assumption guaranteed to be true?
assert( testInstance == ptrToMemBlock );
}
Is the assumption represented by the final assert() guaranteed to always be correct? Or is it conceivable that the compiler might decide to construct the Test instance, say a few bytes after the start of the memory block I specified in the placement-new call?
Note that I'm asking specifically about POD types here. I know that things can get iffy if multiple inheritance and stuff like that gets involved.
This assertion will always hold, because new is required to return blocks of memory with MAXIMUM possible alignment. BTW - your first assert() is worthless, as normal new does not return nullptr - it throws or aborts, only "nothrow new" can return nullptr.
Yes, the the last assert is guaranteed to hold, because this form of placement-new must always return the passed pointer, not using any space for itself:
5.3.4 New [expr.new]
8 A new-expression may obtain storage for the object by calling an allocation function (3.7.4.1). [...]
10 An implementation is allowed to omit a call to a replaceable global allocation function (18.6.1.1, 18.6.1.2). When it does so, the storage is instead provided by the implementation or provided by extending the allocation of another new-expression. [...]
11 When a new-expression calls an allocation function and that allocation has not been extended, the newexpression passes the amount of space requested to the allocation function as the first argument of type std::size_t. That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array.
[...]
Your new-expression calls the global placement-new allocation-function.
That is a non-replacable function, thus the allocation cannot be extended or omitted.
Also, you are not allocating an array but a single object, thus no padding of the request may occur at all.
18.6.1.3 Placement forms [new.delete.placement]
1 These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library (17.6.4). The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.
void* operator new(std::size_t size, void* ptr) noexcept;
2 Returns: ptr.
3 Remarks: Intentionally performs no other action.
And this guarantees that the allocation-function returns the passed pointer unchanged.
Yes, the assertion will hold. Any new expression creating a single object must request exactly sizeof(Test) bytes of storage from the allocation function; and so it must place the object at the start of that storage in order to have enough room.
Note: This is based on the specification of a new-expression in C++11. It looks like C++14 will change the wording, so the answer may be different in the future.

Passing null pointer to placement new

The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification:
void* operator new (std::size_t size, void* ptr) noexcept;
This function does nothing except return ptr; so it is reasonable for it to be noexcept, however according to 5.3.4 [expr.new] ¶15 this means that the compiler must check it doesn't return null before invoking the object's constructor:
-15-
[Note: unless an allocation function is declared with a non-throwing exception-specification (15.4), it indicates failure to allocate storage by throwing a std::bad_alloc exception (Clause 15, 18.6.2.1); it returns a non-null pointer otherwise. If the allocation function is declared with a non-throwing exception-specification, it returns null to indicate failure to allocate storage and a non-null pointer otherwise. —end note] If the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.
It seems to me that (specifically for placement new, not in general) this null check is an unfortunate performance hit, albeit small.
I've been debugging some code where placement new was being used in a very performance-sensitive code path to improve the compiler's code generation and the check for null was observed in the assembly. By providing a class-specific placement new overload that is declared with a throwing exception-specification (even though it can't possibly throw) the conditional branch was removed, which also allowed the compiler to generate smaller code for the surrounding inlined functions. The result of saying the placement new function could throw, even though it couldn't, was measurably better code.
So I've been wondering whether the null check is really required for the placement new case. The only way it can return null is if you pass it null. Although it's possible, and apparently legal, to write:
void* ptr = nullptr;
Obj* obj = new (ptr) Obj();
assert( obj == nullptr );
I can't see why that would be useful, I suggest it would be better if the programmer had to check for null explicitly before using placement new e.g.
Obj* obj = ptr ? new (ptr) Obj() : nullptr;
Has anyone ever needed placement new to correctly handle the null pointer case? (i.e. without adding an explicit check that ptr is a valid memory location.)
I'm wondering whether it would be reasonable to forbid passing a null pointer to the default placement new function, and if not whether there is some better way to avoid the unnecessary branch, other than trying to tell the compiler the value is not null e.g.
void* ptr = getAddress();
(void) *(Obj*)ptr; // inform the optimiser that dereferencing pointer is valid
Obj* obj = new (ptr) Obj();
Or:
void* ptr = getAddress();
if (!ptr)
__builtin_unreachable(); // same, but not portable
Obj* obj = new (ptr) Obj();
N.B. This question is intentionally tagged micro-optimisation, I am not suggesting that you go around overloading placement new for all your types to "improve" performance. This effect was noticed in a very specific performance-critical case and based on profiling and measurement.
Update: DR 1748 makes it undefined behaviour to use a null pointer with placement new, so compilers are no longer required to do the check.
While I can't see much of a question in there except "Has anyone ever needed placement new to correctly handle the null pointer case?" (I haven't), I think the case is interesting enough to spill some thoughts on the issue.
I consider the standard broken or incomplete wrt the placement new function and requirements to allocation functions in general.
If you look closely at the quoted §5.3.4,13, it implies that every allocation function has to be checked for a returned nullpointer, even if it is not noexcept. Therefore, it should be rewritten to
If the allocation function is declared with a non-throwing exception-specification and returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.
That would not harm the validity of allocation functions throwing exceptions, since they have to obey §3.7.4.1:
[...] If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. [...] The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function).
And §5.3.4,14:
[ Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned and of the requested size. [...] -end note ]
Obviously, a placement new that just returns the given pointer, cannot reasonably check avilable storage size and alignment. Therefore,
§18.6.1.3,1 about placement new says
[...] The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.
(I guess they missed to mention §5.3.4,14 at that place.)
However, together these paragraphs say indirectly "if you pass a garbage pointer to the palcement functions, you get UB, because §5.3.4,14 is violated". So it's up to you to check the sanity of any poitner given to placement new.
In that spirit, and with the rewritten §5.3.4,13, the standard could strip the noexcept from placement new, leading to an addition to that indirect conclusion: "...and if you pass null, you get UB as well". On the other hand, its much less likely to have a misaligned pointer or pointer to too few memory than having a null pointer.
However, this would remove the need for checking against null, and it would fit well to the philosophy "don't pay for what you don't need". The allocation function itself would not need to check, because §18.6.1.3,1 explicitly says so.
To round things up, one could consider adding a second overload
void* operator new(std::size_t size, void* ptr, const std::nothrow_t&) noexcept;
Sadly, proposing this to the committee is unlikely to result in a change, because it would break existing code relying on placement new being ok with null pointers.