In C++ placement new is used to construct an object at a particular memory location or to pass additional arguments to an allocation function.
Questions tagged [placement-new]
391 questions
7
votes
1 answer
Is it defined behavior to use placement new to change type of variable that is held by reference_wrapper?
I was messing with placement new, and made this code:
#include
#include
#include
int main()
{
char memory[sizeof(int)]; //memory to hold type
auto ref = std::ref(*new (memory) int{5}); //allocating int in…

xinaiz
- 7,744
- 6
- 34
- 78
7
votes
2 answers
How should I use placement new with a custom allocation API?
I'm working on some memory space with custom allocation and deletion, which are made using a malloc-like interface, that is not under my control (i.e. opaque C-style functions for "allocate n bytes" or "free an allocated ptr"). So, nothing like new…

einpoklum
- 118,144
- 57
- 340
- 684
7
votes
1 answer
Manually calling destructor is not evaluated as referencing variable
Once my class doesn't have destructor defined, following code gives
warning C4189: 'f' : local variable is initialized but not referenced
(f->~Fred() isn't recognised as referencing f)
Is this a bug, or is this standard behaviour?
struct Fred
{
…

relaxxx
- 7,566
- 8
- 37
- 64
7
votes
1 answer
Is previously initialize memory guaranteed to persist after a placement new call?
Say I have the following:
struct A
{
int x;
};
//...
A* aOriginal = new A(); //value construct aOriginal
assert( aOriginal->x == 0 );
A* aSecond = new (aOriginal) A;
assert( aSecond->x == 0 );
Is the second assert guaranteed to hold, even…

Luchian Grigore
- 253,575
- 64
- 457
- 625
6
votes
1 answer
Legality of using delete-expression on a pointer not obtained from usual new
I wonder if this code is legal:
delete new (operator new(1)) char;
This code does the same thing, but doesn't use delete expression on a pointer obtained from placement new:
void* p = operator new(1);
new (p) char;
delete…

Blackteahamburger
- 595
- 1
- 18
6
votes
1 answer
Is accessing a static constexpr member from a placement new a constant expression?
To clarify, is the following program well-formed?
#include
char foo[32];
struct bar {
static constexpr int foobar = 42;
};
int main()
{
auto p = new (foo) bar();
static_assert(p->foobar == 42);
}
gcc and msvc accept,…

Jake Schmidt
- 1,558
- 9
- 16
6
votes
2 answers
Undead objects ([basic.life]/8): why is reference rebinding (and const modification) allowed?
The "undead" clause
I call the undead clause the C++ rule that after the destruction of an object, if a new object is created at the same address, it can sometimes be considered the same object as the old one. That rule always existed in C++ but…

curiousguy
- 8,038
- 2
- 40
- 58
6
votes
1 answer
Is it possible to use placement-new to change the type of a polymorphic object?
Is it possible to use placement-new to change the type of a polymorphic object? if not, what exactly in the standard prohibits it?
Consider this code:
#include
struct Animal {
Animal();
virtual ~Animal();
virtual void breathe();
…

TheCppZoo
- 1,219
- 7
- 12
6
votes
2 answers
Placement-new an STL container and destroying it safely afterwards
This code implements an unrestricted union which provides access by name and by index to any of its three members.
Since std::string is non-trivially constructed and destroyed, I need to provide special constructor and destructor for the…

GetFree
- 40,278
- 18
- 77
- 104
6
votes
4 answers
Placement-new vs gcc 4.4.3 strict-aliasing rules
I've got some code that I've been using successfully for some years to implement a "variant-type object"; that is, a C++ object that can hold a values of various types, but only uses (approximately) as much memory as the largest of the possible…

Jeremy Friesner
- 70,199
- 15
- 131
- 234
6
votes
1 answer
Parenthesis around placement new operator for arrays
Playing around with placement new for arrays, I came up (by chance/mistake) with the following code:
#include
struct X{};
int main()
{
char buf[256];
std::size_t n = 10;
X* p = new (buf) (X[n]); // incorrect way, parenthesis by…

vsoftco
- 55,410
- 12
- 139
- 252
6
votes
1 answer
std::experimental::optional implementation: Constexpr constructor confusion
While implementing std::experimental::optional (cppreference.com) I got confused by the specification of a specific constructor, namely:
constexpr optional( const T& value ); // (4)
(Source)
This constructor allows optional, for a trivially…

nshct
- 1,197
- 9
- 29
6
votes
1 answer
Is it safe to use placement new on 'this' pointer
Current Implementation
I have a class containing unique_ptr fields which depend on one other:
class ResourceManager {
ResourceManager() {}
ResourceManager(A* a_ptr) :
b_ptr(new B(a)),
c_ptr(new C(b_ptr.get())) {}
ResourceManager&…

WaelJ
- 2,942
- 4
- 22
- 28
6
votes
3 answers
Is it dangerous to use placement new on an old object without explicitly calling the destructor first?
I would like to recycle memory for an object rather than deallocating and reconstructing it. Is the following usage of "placement new" safe, assuming that Foo in practice does not contain pointers (but might contain functions)?
Also, is the final…

merlin2011
- 71,677
- 44
- 195
- 329
6
votes
0 answers
C++ atomic "compare and set to zero or increment"
Consider the following (contrived) memory arena (pool):
template
class Arena {
public:
Arena(size_t size)
: m_buffer(new char[size * sizeof(T)]),
m_next_available(0),
m_size(size) { }
void* placement() {
…

Escualo
- 40,844
- 23
- 87
- 135