placement-new

std::launder and strict aliasing rule

倖福魔咒の 提交于 2019-12-21 12:19:46
问题 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

Is it well-defined/legal to placement-new multiple times at the same address?

て烟熏妆下的殇ゞ 提交于 2019-12-21 07:21:24
问题 (Note: this question was motivated by trying to come up with preprocessor hackery to generate a no-op allocation to answer this other question: Macro that accept new object ...so bear that in mind!) Here's a contrived class: class foo { private: int bar; public: foo(int bar) : bar (bar) { std::cout << "construct foo #" << bar << std::endl; } ~foo() { std::cout << "destruct foo #" << bar << std::endl; } }; ...which I will allocate like this: // Note: for alignment, don't use char* buffer with

Undead objects ([basic.life]/8): why is reference rebinding (and const modification) allowed?

。_饼干妹妹 提交于 2019-12-21 05:03:22
问题 The "undead" clause I call the undead clause the C++ rule that after the destruction of an object, if a new object is created at the same address, it can sometimes be considered the same object as the old one. That rule always existed in C++ but with some changes on the additional conditions. I was made to read the latest undead clause by this question. The revised conditions in Lifetime [basic.life]/8 are: (8.1) the storage for the new object exactly overlays the storage location which the

How does placement new know which layout to create?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 04:07:11
问题 #include <iostream> #include <typeinfo> struct A { int a; }; struct B : virtual A { int b; }; struct C : virtual A { int c; }; struct D : B,C { int d; }; int main() { D complete; B contiguous; B & separate = complete; B * p[2] = {&separate, &contiguous}; // two possible layouts for B: std::cout<< (int)((char*)(void*) &p[0]->a -(char*)(void*)&p[0]->b)<<" "<< sizeof(*p[0])<< "\n"; std::cout<< (int)((char*)(void*) &p[1]->a -(char*)(void*)&p[1]->b)<<" "<< sizeof(*p[1])<< "\n"; alignas(B) char

Is it OK to discard placement new return value when initializing objects

旧街凉风 提交于 2019-12-19 18:28:10
问题 This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&A for it. Placement new can be used to initialize objects at allocated storage, e.g., using vec_t = std::vector<int>; auto p = (vec_t*)operator new(sizeof(vec_t)); new(p) vec_t{1, 2, 3}; // initialize a vec_t at p According to cppref, Placement new If placement_params are provided, they are passed to

Is it OK to discard placement new return value when initializing objects

倖福魔咒の 提交于 2019-12-19 18:28:08
问题 This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&A for it. Placement new can be used to initialize objects at allocated storage, e.g., using vec_t = std::vector<int>; auto p = (vec_t*)operator new(sizeof(vec_t)); new(p) vec_t{1, 2, 3}; // initialize a vec_t at p According to cppref, Placement new If placement_params are provided, they are passed to

How to implement a simple container with placement new and emplace functionality?

元气小坏坏 提交于 2019-12-19 08:08:09
问题 I need to implement a container to hold an amount of elements and for some reason, it has to work without any heap allocation. Another requirement is, that the container elements should not be copied or moved in any way. They have to constructed directly into the memory allocated by the container. For that, I decided to use placement new and delegate the memory management completely to the container implementation (found some useful information about placement new at drdobbs). A running

placement new to defer to a different constructor

不羁岁月 提交于 2019-12-18 19:27:11
问题 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(); } } 回答1: 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

Reusing data member storage via placement new during enclosing object's lifetime

佐手、 提交于 2019-12-18 18:09:18
问题 This bounty has ended . Answers to this question are eligible for a +100 reputation bounty. Bounty grace period ends in 15 hours . walnut is looking for an answer from a reputable source . This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.) This question is a slight modification of the original question removing the issue of special rules during construction

Reusing data member storage via placement new during enclosing object's lifetime

三世轮回 提交于 2019-12-18 18:09:12
问题 This bounty has ended . Answers to this question are eligible for a +100 reputation bounty. Bounty grace period ends in 15 hours . walnut is looking for an answer from a reputable source . This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.) This question is a slight modification of the original question removing the issue of special rules during construction