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
8
votes
3 answers
Is modifying the internal bytes of a const object undefined behavior in case it contains another object constructed by placement new?
Consider the following example:
#include
struct FunctionObject
{
int operator()() // non-const, modifies the state of this object
{
i += 1;
j += 2;
return i + j;
}
int i = 0;
int j = 0;
};
struct…

Taras
- 488
- 3
- 15
8
votes
1 answer
Were all implementations of std::vector non-portable before std::launder?
When emplace_back() is called on std::vector instance, an object is created in a previously allocated storage. This can be easily achieved with placement-new, which is perfectly portable.
But now, we need to access the emplaced element without…

Adrian
- 987
- 5
- 13
8
votes
1 answer
At what point does the lifetime of a trivial type created by placement-new start?
During a dive into dynamic memory, it occurred to me it appears contradictory how trivial types begin their lifetime. Consider the snippet
void* p = ::operator new(sizeof(int)); // 1
// 2
new (p) int; // 3
When does the int start its…

Passer By
- 19,325
- 6
- 49
- 96
8
votes
2 answers
How to implement a simple container with placement new and emplace functionality?
I need to implement a container to hold an amount of elements and for some reason, it has to work without any heap allocation. Another requirement is, that the container elements should not be copied or moved in any way. They have to constructed…

Joe
- 3,090
- 6
- 37
- 55
8
votes
1 answer
C++ Is constructing object twice using placement new undefined behaviour?
I have come across some code which has horrified me.
Essentially it follows this pattern :
class Foo
{
public:
//default constructor
Foo(): x(0), ptr(nullptr)
{
//do nothing
}
//more interesting constructor
Foo(…

David Woo
- 749
- 4
- 13
8
votes
1 answer
Placement new and inheritance
Good evening everyone.
A code snippet will be worth a thousand words :
// Storage suitable for any of the listed instances
alignas(MaxAlign ::value)
char storage[MaxSize ::value];
// Instanciate one…

Quentin
- 62,093
- 7
- 131
- 191
8
votes
2 answers
C++ strict aliasing when not using pointer returned by placement new
Can this potentially cause undefined behaviour?
uint8_t storage[4];
// We assume storage is properly aligned here.
int32_t* intPtr = new((void*)storage) int32_t(4);
// I know this is ok:
int32_t value1 = *intPtr;
*intPtr = 5;
// But can one of…

rsp1984
- 1,877
- 21
- 23
8
votes
2 answers
operator new(n) versus new unsigned char[n] for placement new
I'm allocating memory that will later be used for constructing objects with placement new. Should I be using operator new(n), or should I be using new unsigned char[n]? Why?

user541686
- 205,094
- 128
- 528
- 886
8
votes
2 answers
How to directly read a huge chunk of memory into std::vector?
I have a huge contiguous array x that I fread from a file.
How do I drop this chunk into a std::vector<>? In other words, I prefer to have the result to be in std::vector<> rather than the array, but I want the resultant C++ code to be as efficient…

kfmfe04
- 14,936
- 14
- 74
- 140
7
votes
2 answers
invoking copy constructor inside other constructor
#include
#include
#include
#include
class A
{
public:
std::string s;
A()
{
s = "string";
new(this)A(*this);
}
};
int main()
{
A a;
std::cout<

eXXXXXXXXXXX2
- 1,540
- 1
- 18
- 32
7
votes
2 answers
What's wrong with this use of placement new[]? do
Consider the program below. It has been simplified from a complex case. It fails on deleting the previous allocated memory, unless I remove the virtual destructor in the Obj class. I don't understand why the two addresses from the output of the…

Martin
- 9,089
- 11
- 52
- 87
7
votes
2 answers
When I perform placement new on trivial object, Is it guaranteed to preserve the object/value representation?
struct A
{
int x;
}
A t{};
t.x = 5;
new (&t) A;
// is it always safe to assume that t.x is 5?
assert(t.x == 5);
As far as I know, when a trivial object of class type is created, the compiler can omit the call of explicit or implicit default…

Eunho Choi
- 167
- 6
7
votes
1 answer
Calling `std::vector::data()` on `A` with const or reference fields, before C++20
This is a followup on the answers for placement new on a class with reference field.
Calling std::vector::data() on type A that has reference or const fields, returns a pointer to objects that may be changed through the original vector by…

Amir Kirsh
- 12,564
- 41
- 74
7
votes
2 answers
Is using placement-new, copying the storage then accessing the value undefined behavior?
Let S be a struct type which contains a character array data which has the maximum alignment and a fixed size. The idea is that S is able to store any object of type T whose size does not exceed the limit and which is trivially-copy-constructible…

Ambroz Bizjak
- 7,809
- 1
- 38
- 49
7
votes
2 answers
Undefined behaviour on reinitializing object via placement new on this pointer
I saw a presentation on cppcon of Piotr Padlewski saying that the following is undefined behaviour:
int test(Base* a){
int sum = 0;
sum += a->foo();
sum += a->foo();
return sum;
}
int Base::foo(){
new (this) Derived;
return 1;
}
Note:…

Flamefire
- 5,313
- 3
- 35
- 70