Placement new in C++ constructs an object at a memory address you supply instead of allocating new memory. You write new (address) Type(args), and the compiler skips the allocation and only runs the constructor. You need it when you manage raw storage yourself, for example in memory pools, containers such as std::vector, arenas, or memory-mapped buffers.
Because no allocation happens, placement new also gives you a duty: you must end the object's lifetime with an explicit destructor call, and you must free the storage by whatever means you obtained it.
A normal new Widget(1) expression does two things: it calls operator new to get raw memory, then runs the constructor in that memory. Placement new keeps only the second step. The standard library provides the overload void* operator new(std::size_t, void* ptr) noexcept in <new>, which simply returns ptr.
The buffer lives on the stack, so nothing is freed at the end. Only the destructor call is required, and forgetting it leaks any resources the object owns (here, the string's heap storage).
The storage must be large enough (sizeof(T)) and correctly aligned (alignof(T)). A plain char buffer[sizeof(T)] has no alignment guarantee for T, so use alignas(T) as above, or take storage from std::aligned_alloc, operator new(size, std::align_val_t), or an allocator.
Misaligned storage is undefined behaviour and may crash on some CPUs or silently work on others, which makes the bug hard to find.
Placement new separates construction from allocation, so destruction must be separated too. Never call delete on a pointer created with placement new into storage you did not get from new: it would try to free memory that was not allocated that way.
| Step | Normal new / delete | Placement new |
|---|---|---|
| Get memory | done by new | you provide it |
| Construct | done by new | new (p) T(args) |
| Destroy | done by delete | p->~T() |
| Free memory | done by delete | you release it yourself |
If the storage came from operator new(size), release it with the matching operator delete(ptr). For arrays of objects, destroy each element, ideally in reverse order of construction.
Since C++17 you can use std::destroy_at(p) instead of writing p->~T(), which also works in generic code where T may be a scalar type.
A common real use is reserving one block and constructing objects into it later. This example holds up to four Widget objects without any per-object heap allocation.
The destructor destroys objects in reverse order, matching the rule for automatic variables. A production pool would also handle copy and move operations (delete them here), recycled slots, and exceptions thrown from constructors.
Use the pointer returned by placement new, not the original buffer address. Since C++17 the type system treats the new object as a distinct object, and if you reuse the storage for a different object you need std::launder to obtain a valid pointer in some cases (for example when the old object had const members or the storage held an object of the same type that was replaced).
Simply keeping the pointer returned by each placement new expression avoids needing std::launder in nearly all code.
If the constructor throws, the object is not created and the storage stays yours. With the standard placement form no deallocation function is called, so you only need to make sure your own bookkeeping stays correct. Wrap construction in a try block when you track how many slots are live.
The standard placement form is noexcept itself, because it only returns the pointer. It cannot return null, so checking the result for null is pointless.
std::vector allocates raw capacity and constructs elements in it as they are added.std::optional and std::variant: they hold an object in internal storage that is constructed and destroyed on demand.std::pmr resources.For application code, prefer std::vector, std::unique_ptr, std::optional and std::make_shared. Reach for placement new when you are writing the building blocks those types rely on.
Yes. Include <new>; it declares the placement operator new overloads. Without it the expression may fail to compile.
No, not unless the storage came from a matching new. Call the destructor explicitly and release the storage the way you obtained it.
It skips the allocation, so it avoids allocator cost, but the constructor still runs. The gain comes from your storage strategy, such as a pool, not from the keyword itself.
std::construct_at?std::construct_at (C++20) does the same construction in storage but works in constexpr code and takes the target type from the pointer. Prefer it in new code when it fits.
Placement new, new (ptr) T(args), constructs an object in storage you already own. Provide storage with correct size and alignment, use the returned pointer, call the destructor explicitly (or std::destroy_at), and release the storage yourself. It is a tool for memory pools, containers and low-level code, not for everyday object creation.