placement-new

std::launder and strict aliasing rule

好久不见. 提交于 2019-12-04 04:53:05
Consider this code: void f(char * ptr) { auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest // use int_ptr ... } void example_1() { int i = 10; f(reinterpret_cast<char*>(&i)); } void example_2() { alignas(alignof(int)) char storage[sizeof(int)]; new (&storage) int; f(storage); } line of interest with call from example_1: Q1: On the callside the char pointer is aliasing our integer pointer. This is valid. But is it also valid to just cast it back to an int? We know an int is within its lifetime there, but consider the function is defined in another translation unit (with no

Is placement new legally required for putting an int into a char array?

一世执手 提交于 2019-12-02 17:15:28
There seems to be some agreement that you can't willy nilly point (an int*) into a char array because of the C++ aliasing rules. From this other question -- Generic char[] based storage and avoiding strict-aliasing related UB -- it seems that it is allowed to (re-)use storage through placement new. alignas(int) char buf[sizeof(int)]; void f() { // turn the memory into an int: (??) from the POV of the abstract machine! ::new (buf) int; // is this strictly required? (aside: it's obviously a no-op) // access storage: *((int*)buf) = 42; // for this discussion, just assume the cast itself yields

Using placement new to update a reference member?

£可爱£侵袭症+ 提交于 2019-12-01 09:26:24
Is the following code legal in C++? template<typename T> class Foo { public: Foo(T& v) : v_(v) {} private: T& v_; }; int a = 10; Foo<int> f(a); void Bar(int& a) { new (&f)Foo<int>(a); } References are not supposed to be bound twice, right? This is perfectly invalid. [basic.life]/1, emphasis mine: The lifetime of an object of type T ends when: if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or the storage which the object occupies is reused or released. The placement new reuses the storage, ending the lifetime of the object denoted by f . [basic.life]/7:

can placement “new” be used to alter “const” data?

橙三吉。 提交于 2019-12-01 07:47:09
问题 [ This is a follow-up to can memcpy() be used to change “const” member data?. And Idiomatic Way to declare C++ Immutable Classes really gets at the issue, especially this answer "In a language designed around immutable data, it would know it can "move" your data despite its (logical) immutability." ] Given a struct with const members struct point2d { const int x; const int y; }; // can't change to remove "const" A class which holds a pointer to point2d can point to a new point2d instance with

Using placement new to update a reference member?

末鹿安然 提交于 2019-12-01 07:12:34
问题 Is the following code legal in C++? template<typename T> class Foo { public: Foo(T& v) : v_(v) {} private: T& v_; }; int a = 10; Foo<int> f(a); void Bar(int& a) { new (&f)Foo<int>(a); } References are not supposed to be bound twice, right? 回答1: This is perfectly invalid. [basic.life]/1, emphasis mine: The lifetime of an object of type T ends when: if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or the storage which the object occupies is reused or

STL Containers allocation placement new

非 Y 不嫁゛ 提交于 2019-12-01 05:42:15
I couldn't find an exact answer to this question and hence posting here. When I think of vector, it needs to build objects in a contiguous memory location. This means that vector keeps memory allocated and have to do an in-place construction (=placement new) of objects being pushed into it. Is this a valid assumption? Also, does this mean the container is manually invoking the destructor rather than calling delete? Are there any other assumptions that I am missing here? Does this mean I can assume that even a custom written new for the object may not be invoked if I chose to write? Also it

Is it legal to use placement new on initialised memory?

好久不见. 提交于 2019-12-01 02:46:58
问题 I am exploring the possibility of implementing true (partially) immutable data structures in C++. As C++ does not seem to distinguish between a variable and the object that variable stores, the only way to truly replace the object (without assignment operation!) is to use placement new: auto var = Immutable(state0); // the following is illegal as it requires assignment to // an immutable object var = Immutable(state1); // however, the following would work as it constructs a new object // in

placement new to defer to a different constructor

霸气de小男生 提交于 2019-11-30 18:09:17
Is this safe? I'm not using any virtual functions in my actual implementation, but I'm tempted to believe that even if I was, it would still be safe. class Foo { Foo() { // initialize things } Foo( int ) { new ( this ) Foo(); } } By the time you enter the open curly brace of the Foo(int) constructor, all class members have had their constructor called. If you then force a call to another constructor with placement new, you're overwriting the current state of the class. This basically means all members have their constructors called twice - if something does new in its constructor, you leak

Do we need to explicitly call the destructor for the “simple POD classes” allocated with “placement new”?

社会主义新天地 提交于 2019-11-30 16:51:53
问题 Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we don't call the explicit destructor on p ? I don't think it is undefined behavior or memory leak. Is there any problem with reusing buffer ? 回答1: For a POD-type or a class with a trivial destructor: no. The lifetime of the object will end when the storage for the object is released or reused. You don't have

Placement new and uninitialized POD members

删除回忆录丶 提交于 2019-11-30 11:32:57
Does the C++ standard guarantee that uninitialized POD members retain their previous value after a placement new? Or more precisely, will the following assert always be satisfied according to C++11? #include <cstdlib> #include <cassert> struct Foo { int alpha; // NOTE: Uninitialized int beta = 0; }; int main() { void* p = std::malloc(sizeof (Foo)); int i = some_random_integer(); static_cast<Foo*>(p)->alpha = i; new (p) Foo; assert(static_cast<Foo*>(p)->alpha == i); } Is the answer the same for C++03? Lightness Races in Orbit Does the C++ standard guarantee that uninitialized POD members retain