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
27
votes
5 answers

Do I really have to worry about alignment when using placement new operator?

I read this When should I worry about alignment? but I am still do not know if I have to worry about not aligned pointer returned by placement new operator - like in this example: class A { public: long double a; long long b; A() : a(1.3),…
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
22
votes
2 answers

Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?

I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include int main() { std::vector foo(10); foo.~vector(); return 0; // Oops, destructor will be…
yeputons
  • 8,478
  • 34
  • 67
22
votes
7 answers

Using new (this) to reuse constructors

This came up recently in a class for which I am a teaching assistant. We were teaching the students how to do copy constructors in c++, and the students who were originally taught java asked if you can call one constructor from another. I know the…
Brandon Bodnar
  • 8,202
  • 2
  • 36
  • 42
20
votes
4 answers

How to delete object constructed via placement new operator?

char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
20
votes
2 answers

Is it safe to call placement new on `this` for trivial object?

I know that this question was asked several times already but I couldn't find an answer for this particular case. Let's say I have a trivial class that doesn't own any resources and has empty destructor and default constructor. It has a handful of…
Amomum
  • 6,217
  • 8
  • 34
  • 62
19
votes
2 answers

Why is this code trying to call the copy constructor?

I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here. I am wondering why…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
19
votes
3 answers

Is placement new legally required for putting an int into a char array?

There seems to be some agreement that you can't willy nilly point (an int*) into a char array because of the C++ aliasing rules. From this other question -- Generic char[] based storage and avoiding strict-aliasing related UB -- it seems that it is…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
18
votes
3 answers

Can placement-new and vector::data() be used to replace elements in a vector?

There are two existing questions about replacing vector elements that are not assignable: C++ Use Unassignable Objects in Vector How to push_back without operator=() for const members? A typical reason for an object to be non-assignable is that…
jogojapan
  • 68,383
  • 11
  • 101
  • 131
17
votes
5 answers

Mixing operator new[] and placement new with ordinary delete[]

Just out of curiosity, is the following legal? X* p = static_cast(operator new[](3 * sizeof(X))); new(p + 0) X(); new(p + 1) X(); new(p + 2) X(); delete[] p; // Am I allowed to use delete[] here? Or is it undefined behavior? Similarly: X* q…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
17
votes
3 answers

C++ Volatile Placement New

How does one do a placement new operation on a volatile pointer. For example, I want to do something like this: volatile SomeStruct Object; volatile SomeStruct* thing = &Object; new (thing) SomeStruct(/*arguments to SomeStruct's constructor*/); I…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
17
votes
4 answers

CUDA: Wrapping device memory allocation in C++

I'm starting to use CUDA at the moment and have to admit that I'm a bit disappointed with the C API. I understand the reasons for choosing C but had the language been based on C++ instead, several aspects would have been a lot simpler, e.g. device…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
17
votes
1 answer

'operator new' : function does not take 2 arguments

I cannot seem to get my placement new to work for some reason. Based on this question, I have set this up correctly. However, I continue to get the error: 'operator new' : function does not take 2 arguments Here is my code: char * p = new char…
Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
16
votes
2 answers

std::optional implemented as union vs char[]/aligned_storage

While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template class optional { // ... private: bool has_value_; aligned_storage
Ron
  • 1,989
  • 2
  • 17
  • 33
16
votes
2 answers

Is there a (semantic) difference between the return value of placement new and the casted value of its operand?

Is there a (semantic) difference between the return value of placement new and the casted value of its operand? struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast(buffer); Does a and b differ in some…
geza
  • 28,403
  • 6
  • 61
  • 135
16
votes
3 answers

How C++ placement new works?

This question is to confirm I understood the concept right and take expert opinion on the style of usages and possible optimization. I am trying to understand "placement new" and following is the program I came up with... #include
vikrant
  • 393
  • 4
  • 15
1
2
3
26 27