Questions tagged [placement-new]

In C++ placement new is used to construct an object at a particular memory location or to pass additional arguments to an allocation function.

391 questions
15
votes
4 answers

Undefined behavior or memory leak when using placement-new

I am learning about placement-new in C++ using the books listed here. Now, to look at some examples, I came across the following snippet in one of the SO post that claims that it (the given example) has undefined behavior : For example, this has…
Kal
  • 475
  • 1
  • 16
15
votes
3 answers

Overriding an object in memory with placement new

I have an object which I want to 'transform' into another object. For this I am using a placement new on the first object which creates a new object of the other type on top of its own address. Consider the following code: #include #include…
Guy Yafe
  • 991
  • 1
  • 8
  • 26
15
votes
12 answers

What are uses of the C++ construct "placement new"?

I just learned about the C++ construct called "placement new". It allows you to exactly control where a pointer points to in memory. It looks like this: #include // Must #include this to use "placement new" #include "Fred.h" //…
Scottie T
  • 11,729
  • 10
  • 45
  • 59
15
votes
2 answers

Placement new and uninitialized POD members

Does the C++ standard guarantee that uninitialized POD members retain their previous value after a placement new? Or more precisely, will the following assert always be satisfied according to C++11? #include #include struct Foo…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
14
votes
3 answers

Is it well-defined/legal to placement-new multiple times at the same address?

(Note: this question was motivated by trying to come up with preprocessor hackery to generate a no-op allocation to answer this other question: Macro that accept new object ...so bear that in mind!) Here's a contrived class: class foo { private: …
14
votes
3 answers

Why can't a placement-new expression be a constant expression?

According to [expr.const]/5.18: An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following: a new-expression ([expr.new]), unless…
14
votes
2 answers

After placement-new into buffer, do buffer and instance have same void* address?

Please see the below code: unsigned char* p = new unsigned char[x]; CLASS* t = new (p) CLASS; assert((void*)t == (void*)p); Can I assume (void*)t == (void*)p?
ravin.wang
  • 1,122
  • 1
  • 9
  • 26
14
votes
4 answers

placement new + array +alignment

SomeObj* Buffer; char* BufferPtr = MemoryManager::giveMeSomeBytes(resX*resY*sizeof(SomeObj)); Buffer = new(BufferPtr) SomeObj[resX*resY]; when I step past these lines with the debugger, it shows me the…
Mat
  • 2,713
  • 5
  • 25
  • 25
14
votes
5 answers

What is this second new?

What is the second line? (Seen while answering another question.) int * x = new int [1] ; int * y = new (x) int; After the second line x and y have the same value (point to a same place). What's the difference between y = x and the second line? …
Amir Zadeh
  • 3,481
  • 2
  • 26
  • 47
14
votes
4 answers

Do we need to explicitly call the destructor for the "simple POD classes" allocated with "placement new"?

Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we don't call the explicit destructor on p? I don't think it…
iammilind
  • 68,093
  • 33
  • 169
  • 336
13
votes
6 answers

Why should I use placement new?

As it seems, placement new creates a new object on a preallocated memory, so does it mean that it would take less time? Looks like it's faster then allocating using the old ordinary new. Then, if this is so convenient and faster, why not use…
JAN
  • 21,236
  • 66
  • 181
  • 318
13
votes
1 answer

Placement new and assignment of class with const member

Why is that undefined behaviour? struct s { const int id; // <-- const member s(int id): id(id) {} s& operator =(const s& m) { return *new(this) s(m); // <-- undefined behavior? } }; (Quote from the standard…
DaBler
  • 2,695
  • 2
  • 26
  • 46
13
votes
3 answers

Destructor not called after destroying object placement-new'ed

I had no clue why this doesn't work. The following Function is created by placement new. A function is provided that checks whether it should be destructed, and if so, calls its destructor manually. Here is the testcase where it seems the…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
13
votes
1 answer

Manually constructing a trivial base class via placement-new

Beware, we're skirting the dragon's lair. Consider the following two classes: struct Base { std::string const *str; }; struct Foo : Base { Foo() { std::cout << *str << "\n"; } }; As you can see, I'm accessing an uninitialized pointer. Or…
Quentin
  • 62,093
  • 7
  • 131
  • 191
13
votes
1 answer

Placement new and non-default constructors

Can I call the C++ placement new on constructors with parameters? I am implementing a custom allocator and want to avoid having to move functionality from non-default constructors into an init function. class CFoo { public: int foo; CFoo() …
Chris Masterton
  • 2,197
  • 4
  • 24
  • 30
1 2
3
26 27