placement-new

CUDA: Wrapping device memory allocation in C++

别说谁变了你拦得住时间么 提交于 2019-11-29 00:58:34
问题 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 memory allocation (via cudaMalloc ). My plan was to do this myself, using overloaded operator new with placement new and RAII (two alternatives). I'm wondering if there are any caveats that I haven't noticed so far. The code seems to work but I'm

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

谁说胖子不能爱 提交于 2019-11-28 17:48:11
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); } 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 an object has ended and before the storage which the object occupied is reused or released, a new object is

malloc & placement new vs. new

蹲街弑〆低调 提交于 2019-11-28 16:59:47
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_object * my_array=(my_object *)malloc(sizeof(my_object)*MY_ARRAY_SIZE); if (my_object==NULL) throw MEMORY

Overwriting an object with an object of same type

只谈情不闲聊 提交于 2019-11-28 13:51:27
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 constructed at the exact same address, so the bit pattern of p represents the address of the new object.

Is `new (this) MyClass();` undefined behaviour after directly calling the destructor?

心不动则不痛 提交于 2019-11-28 13:27:44
In this question of mine , @DeadMG says that reinitializing a class through the this pointer is undefined behaviour. Is there any mentioning thereof in the standard somewhere? Example: #include <iostream> class X{ int _i; public: X() : _i(0) { std::cout << "X()\n"; } X(int i) : _i(i) { std::cout << "X(int)\n"; } ~X(){ std::cout << "~X()\n"; } void foo(){ this->~X(); new (this) X(5); } void print_i(){ std::cout << _i << "\n"; } }; int main(){ X x; x.foo(); // mock random stack noise int noise[20]; x.print_i(); } Example output at Ideone (I know that UB can also be "seemingly correct behaviour")

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

泄露秘密 提交于 2019-11-28 12:08:56
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 simply do the following: Applepie& Applepie::operator=( const Applepie &copy) { if( this != &copy) {

placement new + array +alignment

為{幸葍}努か 提交于 2019-11-28 09:58:08
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 array elements using theyr default constructors on the allocated memory and return a pointer to the first

Is it dangerous to use placement new on an old object without explicitly calling the destructor first?

雨燕双飞 提交于 2019-11-28 09:22:10
问题 I would like to recycle memory for an object rather than deallocating and reconstructing it. Is the following usage of "placement new" safe, assuming that Foo in practice does not contain pointers (but might contain functions)? Also, is the final delete call safe, and will it correctly call the destructor on the second "new" object, and correctly free the memory afterwards? #include <new> struct Foo { int hello; int world; }; int main() { Foo* foo = new Foo; // Do something with foo // Done

How to delete object constructed via placement new operator?

我的未来我决定 提交于 2019-11-28 08:19:39
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 with code I'm going to write. So please don't give answers like (Why use placement new instead of non

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

和自甴很熟 提交于 2019-11-27 19:51:56
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 example, the this pointer of Fred will be equal to place . I've seen it used in our team's code once or