Search code examples
c++placement-new

What is a placement new?


I have some questions about placement new:

int x;
int* p = new(&x) int{10};
std::cout << x; // 10
  • When we say placement new, do we refer to new expression or operator new (function)?

     void* operator new(std::size_t, void*. int); // is this a placement new?
    

I am so confused! sometimes I find "new operator" other times I find "operator new"?!

Sometimes I find an overload of operator new function referred to as placement new:

   void* operator new(std::size_t, void*, int) // placement new?

Here is another example:

    void* operator new(std::size_t, std::size_t){
        std::cout << "operator new(std::size_t, std::size_t)\n";
        return nullptr;
    }

    int* p = new(5u)int;
  • Is this a placement new even the first parameter is not a pointer?

Solution

  • What is a placement new?

    It constructs a dynamic object into provided area of storage.

    When we say placement new, do we refer to new expression

    Yes, but specifically to a new expression where the placement parameter has not been omitted.

    "Placement syntax" is also used to refer to this.

    or operator new (function)?

    ...also yes although perhaps less often. It depends on context. New expression will call one of the new operators before initialising the object into the address returned by the operator. Placement new expression calls a placement operator new.

    The standard placement operator new does nothing and returns the pointer argument unchanged.


    Example:

    Following is a new expression where placement new parameter has been omitted. This allocates storage (non-placement operator new will be invoked) and creates an object into that storage.

    T* p1 = new T();
    //         ^ no placement parameter
    delete p1; // don't forget to clean up
    

    Following is not a new expression, but a call to a non-placement global operator new.

    void* storage = ::operator new(sizeof(T), alignof(T));
    

    Following is a new expression with a placement parameter. This is the "placement new". It creates an object without allocating storage.

    T* p2 = new (storage) T(); // must include <new>
    //          ^^^^^^^^^ placement parameter
    p2->~T(); // don't forget to clean up
    ::operator delete(storage);
    

    Note: Placement new doesn't necessarily have to construct the object into dynamically allocated memory.

    P.S. Starting from C++11, std::allocator_traits<std::allocator>::construct can be used in place of placement new, and since C++20 there is std::construct_at. Unlike placement new, these alternatives are constexpr (since C++20).


    Is this a placement new even the first parameter is not a pointer?

    Yes, the new(5u)int is still a placement new expression, regardless of the type of the placement parameter.

    There is no standard placement operator new accepting anything other than void*, but the user defined placement operator new such as one shown in the question would allow using such expression.

    This is rather obscure feature of the language. I've never seen this used in practice.

    P.S. The shown operator new is practically broken because it returns null.