placement-new

placement new + array +alignment

时光总嘲笑我的痴心妄想 提交于 2019-11-27 03:19:24
问题 SomeObj<unsigned int>* Buffer; char* BufferPtr = MemoryManager::giveMeSomeBytes(resX*resY*sizeof(SomeObj<unsigned int>)); Buffer = new(BufferPtr) SomeObj<unsigned int>[resX*resY]; when I step past these lines with the debugger, it shows me the values for the variables Buffer and BufferPtr: BufferPtr: 0x0d7f004c Buffer: 0x0d7f0050 I don't really understand why those values differ. The way I understand it, placement new should use the memory starting at address 'BufferPtr' to initialize the

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

。_饼干妹妹 提交于 2019-11-26 22:51:48
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<Foo *>(buffer); Does a and b differ in some way? EDIT: Based on DaBler's comment, this question tells that there is a difference, if const/reference members used: Placement new and assignment of class with const member So, my little-bit updated question: Does a and b differ in any way, if Foo doesn't have const or reference members? Only a can safely be used to directly access the Foo object

How to properly free the memory allocated by placement new?

我只是一个虾纸丫 提交于 2019-11-26 22:14:46
问题 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

Using operator new and operator delete with a custom memory pool/allocator

微笑、不失礼 提交于 2019-11-26 22:05:00
问题 I'm working on a memory pool/memory allocator implementation and I am setting it up in a manor where only a special "Client" object type can draw from the pool.The client can either be constructed directly onto the pool, or it can use the pool for dynamic memory calls or it could in theory do both. I would like to be able to overload operator new and operator delete in a way that would call my pools "alloc()" and "free()" functions in order to get the memory needed for the object to construct

Why is this code trying to call the copy constructor?

江枫思渺然 提交于 2019-11-26 19:06:20
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 the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst;

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

半腔热情 提交于 2019-11-26 18:18:16
问题 Possible Duplicate: C++'s “placement new” What is an in-place constructor in C++? e.g. Datatype *x = new(y) Datatype(); 回答1: 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

Destroy and then construct new object using the same variable

懵懂的女人 提交于 2019-11-26 15:26:54
Sometimes it's nice to start over. In C++ I can employ this following simple manoeuvre: { T x(31, Blue, false); x.~T(); // enough with the old x ::new (&x) T(22, Brown, true); // in with the new! // ... } At the end of the scope, the destructor will run once again and all seems well. (Let's also say T is a bit special and doesn't like being assigned, let alone swapped.) But something tells me that it's not always without risk to destroy everything and try again. Is there a possible catch with this approach? I think the only way to make this really safe to use is to require the called

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

邮差的信 提交于 2019-11-26 14:23:01
问题 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

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

社会主义新天地 提交于 2019-11-26 08:27:46
问题 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<Foo *>(buffer); Does a and b differ in some way? EDIT: Based on DaBler\'s comment, this question tells that there is a difference, if const/reference members used: Placement new and assignment of class with const member So, my little-bit updated question: Does a and b differ in any way, if Foo

Why is this code trying to call the copy constructor?

南笙酒味 提交于 2019-11-26 06:48:01
问题 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 the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a