placement-new

Object access using reinterpret_cast for “struct {double, int}”-like object

泄露秘密 提交于 2020-01-04 05:21:09
问题 Accessing objects via reinterpret_cast ed pointers and related UB has been extensively discussed here. After reading questions and answers, I'm still not sure about proper using uninitialized memory with POD types. Suppose I want to "emulate" struct { double d; int i; }; by manually allocating memory for data members and suppose (for simplicity) that no padding is needed before i . Now, I do this: // (V1) auto buff = reinterpret_cast<char*>(std::malloc(sizeof(double) + sizeof(int))); auto d

Does encapsulated char array used as object breaks strict aliasing rule

风格不统一 提交于 2020-01-04 03:03:21
问题 Do the following class break the strict aliasing rule: template<typename T> class store { char m_data[sizeof(T)]; bool m_init; public: store() : m_init(false) {} store(const T &t) : init(true) { new(m_data) T(t); } ~store() { if(m_init) { get()->~T(); } } store &operator=(const store &s) { if(m_init) { get()->~T(); } if(s.m_init) { new(m_data) T(*s.get()); } m_init = s.m_init; } T *get() { if (m_init) { return reinterpret_cast<T *>(m_data); } else { return NULL; } } } My reading of a standard

c++ self-defined placement new and placement delete invoking

妖精的绣舞 提交于 2020-01-03 02:27:23
问题 I want to define my own placement new and placement delete(taking extra parameters), and I found I could invoke the placement correctly, while I couldn't access the placement delete. Could anyone tell me whether I define the placement delete incorrectly or I invoke it incorrectly? class A { public: A( int a ) : a(a){} static void* operator new( std::size_t, int ); // the placement new static void operator delete( void*, int )throw(); // the corresponding placement delete private: int a; };

Is such assignment a good idea in C++

若如初见. 提交于 2020-01-02 07:14:14
问题 A lot of classes has assignment operator (operator=) the same code as in destructor and than very similar code of copy constructor. So is it good idea to implement the assignment in such way? Point& operator=(const Point& point) { if(&point != this) { //Call the destructor this->~Point(); //Make the placement new //Assignment is made because some compilers optimise such code as just // new Point; Point* p_n = new (this) Point(point); //We where placing in this place so pointers should be

Is move assignment via destruct+move construct safe?

有些话、适合烂在心里 提交于 2020-01-02 01:55:11
问题 Here's a very easy way to define move assignment for most any class with a move constructor: class Foo { public: Foo(Foo&& foo); // you still have to write this one Foo& operator=(Foo&& foo) { if (this != &foo) { // avoid destructing the only copy this->~Foo(); // call your own destructor new (this) Foo(std::move(foo)); // call move constructor via placement new } return *this; } // ... }; Is this sequence of calling your own destructor followed by placement new on the this pointer safe in

What is this second new?

醉酒当歌 提交于 2020-01-02 00:49:09
问题 What is the second line? (Seen while answering another question.) int * x = new int [1] ; int * y = new (x) int; After the second line x and y have the same value (point to a same place). What's the difference between y = x and the second line? Is it like a constructor or something? 回答1: It's placement new. It constructs a new int in the memory pointed to by x . If you try: int * x = new int [1]; *x = 5; std::cout << *x << std::endl; int * y = new (x) int; *y = 7; std::cout << *x << std::endl

Placement new and assignment of class with const member

做~自己de王妃 提交于 2019-12-28 00:55:33
问题 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. 回答1: 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

Placement new crashing when used with virtual inheritance hierarchy in Visual C++

跟風遠走 提交于 2019-12-24 07:04:07
问题 I am using virtual inheritance with a selection of classes in c++. It is currently crashing on destruction. It seems to compile fine in the online compilers, however when I run in Visual Studio, it crashes. I have a pure virtual base class, which is being inherited virtually by its implementation. I then have a third class that is inheriting from the implementation regularly. I am using an internal system for creating and releasing memory. Under the hood it is using a placement new with a

Is it safe to combine sizeof and placement new?

社会主义新天地 提交于 2019-12-24 01:46:11
问题 Consider the following class: template <class T> class defer { public: template <class ...Args> void construct(Args&&...); T& obj(); ~defer(); private: std::uint8_t memory[sizeof(T)]; T * ptr(); }; template <class T> template <class ...Args> void defer<T>::construct(Args&& ...args) { new(static_cast<void*>(&memory[0])) T(std::forward<Args>(args)...); } template <class T> T& defer<T>::obj() { return *(ptr()); } template <class T> defer<T>::~defer() { ptr()->~T(); } template <class T> T * defer

Does placement new zero out the memory?

拥有回忆 提交于 2019-12-23 22:15:11
问题 I have the following code : struct foo {}; void bar(foo *d) { new(d) foo(*d); } Does the expression new(d) foo(*d) leave the object pointed to by d unchanged? More specifically, is the above true if the class foo and all the objects contained recursively within it only have trivial copy constructors, then does new(d) foo(*d) leave *d unchanged? A situation in which that is not true could be, new first zeroes out the memory before calling the copy constructor. Are there such clauses in the C++