placement-new

assignment of class with const member

谁说胖子不能爱 提交于 2019-11-27 19:23:36
问题 Consider the following code: struct s { const int id; s(int _id): id(_id) {} }; // ... vector<s> v; v.push_back(s(1)); I get a compiler error that 'const int id' cannot use default assignment operator. Q1. Why does push_back() need an assignment operator? A1. Because the current c++ standard says so. Q2. What should I do? I don't want to give up the const specifier I want the data to be copied A2. I will use smart pointers. Q3. I came up with a "solution", which seems rather insane: s&

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

断了今生、忘了曾经 提交于 2019-11-27 19:05:19
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://ideone.com/jBrk8 If this depends on architecture then I am using: linux/powerpc/g++ 4.x.x. [UPDATE] Just

What is an in-place constructor in C++? [duplicate]

谁都会走 提交于 2019-11-27 13:36:54
Possible Duplicate: C++'s “placement new” What is an in-place constructor in C++? e.g. Datatype *x = new(y) Datatype(); This is called the placement new operator. It allows you to supply the memory the data will be allocated in without having the new operator allocate it. For example: Foo * f = new Foo(); The above will allocate memory for you. void * fm = malloc(sizeof(Foo)); Foo *f = new (fm) Foo(); The above will use the memory allocated by the call to malloc . new will not allocate any more. You are not, however, limited to classes. You can use a placement new operator for any type you

Is “rebinding” references in C++ like this legal?

妖精的绣舞 提交于 2019-11-27 10:46:00
问题 Is the following legal in C++? As far as I can tell, Reference has a trivial destructor, so it should be legal. But I thought references can't be rebound legally... can they? template<class T> struct Reference { T &r; Reference(T &r) : r(r) { } }; int main() { int x = 5, y = 6; Reference<int> r(x); new (&r) Reference<int>(y); } 回答1: I think I found the answer in a passage below the "quoted" one that talks about trivial dtor / dtor side effects, namely [basic.life]/7: If, after the lifetime of

malloc & placement new vs. new

徘徊边缘 提交于 2019-11-27 10:19:08
问题 I've been looking into this for the past few days, and so far I haven't really found anything convincing other than dogmatic arguments or appeals to tradition (i.e. "it's the C++ way!" ). If I'm creating an array of objects, what is the compelling reason (other than ease) for using: #define MY_ARRAY_SIZE 10 // ... my_object * my_array=new my_object [MY_ARRAY_SIZE]; for (int i=0;i<MY_ARRAY_SIZE;++i) my_array[i]=my_object(i); over #define MEMORY_ERROR -1 #define MY_ARRAY_SIZE 10 // ... my

Legality of using operator delete on a pointer obtained from placement new

一世执手 提交于 2019-11-27 08:55:54
I'm dang certain that this code ought to be illegal, as it clearly won't work, but it seems to be allowed by the C++0x FCD. class X { /* ... */}; void* raw = malloc(sizeof (X)); X* p = new (raw) X(); // according to the standard, the RHS is a placement-new expression ::operator delete(p); // definitely wrong, per litb's answer delete p; // legal? I hope not Maybe one of you language lawyers can explain how the standard forbids this. There's also an array form: class X { /* ... */}; void* raw = malloc(sizeof (X)); X* p = new (raw) X[1]; // according to the standard, the RHS is a placement-new

Overwriting an object with an object of same type

南楼画角 提交于 2019-11-27 07:58:51
问题 Is the following well defined? #include <iostream> #include <string.h> using namespace std; struct Const { const int i; Const (int i) : i(i) {} int get0() { return 0; } // best accessor ever! }; int main() { Const *q,*p = new Const(1); new (p) Const(2); memcpy (&q, &p, sizeof p); cout << q->i; return 0; } Note that after construction of second Const , p doesn't semantically (intentionally?) points to new object, and the first is gone, so it is usable "as a void* ". But the second object is

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

删除回忆录丶 提交于 2019-11-27 06:36:08
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 Sort of. You can use placement new to run the constructor using already-allocated memory: #include <new> Object1 ooo[2] = {Object1("I'm

Placement new and assignment of class with const member

Deadly 提交于 2019-11-27 04:33:32
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 would be nice). This question arose from this answer . There is nothing that makes the shown code snippet inherently UB. However, it is almost certain UB will follow immediately under any normal usage. From [basic.life]/8 (emphasis mine) If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the

How to properly free the memory allocated by placement new?

…衆ロ難τιáo~ 提交于 2019-11-27 04:23:39
I've been reading somewere that when you use placement new then you have to call the destructor manually. Consider the folowing code: // Allocate memory ourself char* pMemory = new char[ sizeof(MyClass)]; // Construct the object ourself MyClass* pMyClass = new( pMemory ) MyClass(); // The destruction of object is our duty. pMyClass->~MyClass(); As far as I know operator delete normally calls the destructor and then deallocates the memory, right? So why don't we use delete instead? delete pMyClass; //what's wrong with that? in the first case we are forced to set pMyClass to nullptr after we