placement-new

How to delete object constructed via placement new operator?

空扰寡人 提交于 2020-01-27 15:54:04
问题 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 correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship

Placement new on non-pointer variables and class members

不羁岁月 提交于 2020-01-14 16:49:02
问题 Consider the following example: #include <iostream> struct A { int i; A(int i) { this->i = i; } A &operator=(const A &a) = delete; A(const A &a) = delete; }; int main() { A a(1); new(&a) A(5); //a = A(7); // not allowed since = is deleted in A std::cout << a.i << std::endl; } This is a simple example using the placement new operator. Since the copy constructor and assignment operator of struct A have been deleted (for whatever reason), it is not possible to change the object the variable A a

Placement new on non-pointer variables and class members

久未见 提交于 2020-01-14 16:46:31
问题 Consider the following example: #include <iostream> struct A { int i; A(int i) { this->i = i; } A &operator=(const A &a) = delete; A(const A &a) = delete; }; int main() { A a(1); new(&a) A(5); //a = A(7); // not allowed since = is deleted in A std::cout << a.i << std::endl; } This is a simple example using the placement new operator. Since the copy constructor and assignment operator of struct A have been deleted (for whatever reason), it is not possible to change the object the variable A a

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

本秂侑毒 提交于 2020-01-11 15:28:29
问题 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 allowed to (re-)use storage through placement new. alignas(int) char buf[sizeof(int)]; void f() { // turn the memory into an int: (??) from the POV of the abstract machine! ::new (buf) int; // is this strictly required? (aside: it's obviously a no

STL Containers allocation placement new

柔情痞子 提交于 2020-01-11 05:38:09
问题 I couldn't find an exact answer to this question and hence posting here. When I think of vector, it needs to build objects in a contiguous memory location. This means that vector keeps memory allocated and have to do an in-place construction (=placement new) of objects being pushed into it. Is this a valid assumption? Also, does this mean the container is manually invoking the destructor rather than calling delete? Are there any other assumptions that I am missing here? Does this mean I can

treating memory returned by operator new(sizeof(T) * N) as an array

亡梦爱人 提交于 2020-01-10 01:34:07
问题 In C one can allocate dynamic arrays using malloc(sizeof(T) * N) and then use pointer arithmetic to get elements at i offset in this dynamic array. In C++ one can do similar using operator new() in the same way as malloc() and then placement new (for an example one can see solution for item 13 in a book "Exceptional C++: 47 engineering puzzles, programming problems, and solutions" by Herb Sutter). If you don't have one, the summary of the solution for this question would be: T* storage =

C++ Is constructing object twice using placement new undefined behaviour?

拈花ヽ惹草 提交于 2020-01-09 07:48:30
问题 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( FooInitialiser& init): x(0), ptr(nullptr) { x = init.getX(); ptr = new int; } ~Foo() { delete ptr; } private: int x; int* ptr; }; void someFunction( FooInitialiser initialiser ) { int numFoos = MAGIC_NUMBER; Foo* fooArray = new Foo[numFoos]; //allocate an array of default constructed

C++ Is constructing object twice using placement new undefined behaviour?

南笙酒味 提交于 2020-01-09 07:48:26
问题 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( FooInitialiser& init): x(0), ptr(nullptr) { x = init.getX(); ptr = new int; } ~Foo() { delete ptr; } private: int x; int* ptr; }; void someFunction( FooInitialiser initialiser ) { int numFoos = MAGIC_NUMBER; Foo* fooArray = new Foo[numFoos]; //allocate an array of default constructed

Reusing data member storage via placement new

五迷三道 提交于 2020-01-05 07:16:21
问题 Is it allowed to reuse storage of a non-static data member and if so under what conditions? Consider the program #include<new> #include<type_traits> using T = /*some type*/; using U = /*some type*/; static_assert(std::is_object_v<T>); static_assert(std::is_object_v<U>); static_assert(sizeof(U) <= sizeof(T)); static_assert(alignof(U) <= alignof(T)); struct A { T t /*initializer*/; U* u; A() { t.~T(); u = ::new(static_cast<void*>(&t)) U /*initializer*/; } ~A() { u->~U(); ::new(static_cast<void*

Freeing memory allocated from placement new

我怕爱的太早我们不能终老 提交于 2020-01-04 06:25:15
问题 Consider the following code, #include "iostream" #include "conio.h" using namespace std; class sample { private: int i; public: sample(int ii=0) : i(ii){ cout<<"Constructing Object"<<endl; } ~sample() { cout<<"Destructing Object"<<endl; } void* operator new(size_t nSize, void* loc){ cout <<"Inside new"<<endl; cout <<loc<<endl; return loc; } void operator delete(void* ptr){ cout <<"Inside delete"<<endl; free(ptr); } }; int main() { int intArr[2]; sample* samplePtr = new(intArr) sample(5); cout