placement-new

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

我与影子孤独终老i 提交于 2019-12-18 12:15:32
问题 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 its class definition includes const members and therefore has its operator= deleted. std::vector requires that its element type be assignable. And indeed, at least using GCC, neither direct assignment ( vec[i] = x; ), nor a combination of erase() and

Passing null pointer to placement new

孤人 提交于 2019-12-18 10:44:49
问题 The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification: void* operator new (std::size_t size, void* ptr) noexcept; This function does nothing except return ptr; so it is reasonable for it to be noexcept , however according to 5.3.4 [expr.new] ¶15 this means that the compiler must check it doesn't return null before invoking the object's constructor: -15- [ Note: unless an allocation function is declared with a non-throwing

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

☆樱花仙子☆ 提交于 2019-12-18 05:38:32
问题 Just out of curiosity, is the following legal? X* p = static_cast<X*>(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 = new X[3](); (q + 2)->~X(); (q + 1)->~X(); (q + 0)->~X(); operator delete[](q); 回答1: I'm pretty sure both give UB. §5.3.4/12 says the array form of a new expression may add some arbitrary amount of overhead to the amount of memory allocated. The array

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

最后都变了- 提交于 2019-12-18 04:31:35
问题 I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); return 0; // Oops, destructor will be called again on return, double-free. } But, what if we call placement new to "resurrect" the object? #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); new (&foo) std::vector<int>(5); return 0; } More formally: What will happen in C++

How C++ placement new works?

时光毁灭记忆、已成空白 提交于 2019-12-18 02:53:45
问题 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 <iostream> #include <new> class A { int *_a; public: A(int v) {std::cout<<"A c'tor clalled\n";_a= new int(v);} ~A() {std::cout<<"A d'tor clalled\n"; delete(_a);} void testFunction() {std::cout<<"I am a test function &_a = "<<_a<<" a = "<<*_a<<"\n";} }; int main() {

Can I use placement new(this) in operator=?

血红的双手。 提交于 2019-12-17 20:35:40
问题 Background: I have a complicated class with many variables. I have a sound and tested copy constructor: Applepie::Applepie( const Applepie &copy) : m_crust(copy.m_crust), m_filling(copy.m_filling) { } Some of the member variable copy constructors called in the intializer list perform allocation. Question: I need to create operator= . Rather than duplicating the existing constuctor with assignment instead of initialization list, and freeing memory that's being replaced, and etc etc etc, can I

What are uses of the C++ construct “placement new”?

ぐ巨炮叔叔 提交于 2019-12-17 15:46:23
问题 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 <new> // Must #include this to use "placement new" #include "Fred.h" // Declaration of class Fred void someCode() { char memory[sizeof(Fred)]; void* place = memory; Fred* f = new(place) Fred(); // Create a pointer to a Fred(), // stored at "place" // The pointers f and place will be equal ... } (example from C++ FAQ Lite) In this

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

六眼飞鱼酱① 提交于 2019-12-17 15:39:27
问题 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), b(1234) {} }; char buffer[64]; int main() { // (buffer + 1) used intentionally to have wrong alignment A* a = new (buffer + 1) A(); a->~A(); } __alignof(A) == 4 , (buffer + 1) is not aligned to 4 . But everything works fine - full example here: http:/

C++, is it possible to call a constructor directly, without new?

心不动则不痛 提交于 2019-12-17 08:24:42
问题 Can I call constructor explicitly, without using new , if I already have a memory for object? class Object1{ char *str; public: Object1(char*str1){ str=strdup(str1); puts("ctor"); puts(str); } ~Object1(){ puts("dtor"); puts(str); free(str); } }; Object1 ooo[2] = { Object1("I'm the first object"), Object1("I'm the 2nd") }; do_smth_useful(ooo); ooo[0].~Object1(); // call destructor ooo[0].Object1("I'm the 3rd object in place of first"); // ???? - reuse memory 回答1: Sort of. You can use placement

What is the meaning of the below sentence in c++ [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-14 04:16:53
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents, is it the way of typecasting? or what void someCode() { char memory[sizeof(Fred)]; // Line #1 void* place = memory; // Line #2 Fred* f = new(place) Fred(); // Line #3 // The pointers f and place will be equal ... } 回答1: This is a typical usage of Placement new . It allows you to allocate memory and then construct objects at that particular