placement-new

Placement new, return by value and safely dispose temporary copies

别来无恙 提交于 2019-12-14 04:02:45
问题 Due to complicated circumstances (explained in earlier question Constructing an object to return by value elsewhere) I want to return an object by value from function X, but create it in another function Y indirectly called by X. Between them there is 3rd party code in the call stack that won't co-operate in passing the object around. X can only pass pointers to Y and receive a pointer back. I've come up with a solution using placement new but mainly worry whether it's portable, doesn't

Using placement new, malloc, and free

耗尽温柔 提交于 2019-12-13 08:28:34
问题 Basically, I have a block of memory allocated using malloc that I want to start placing objects into using placement new. I know that the destructors for these objects will have to be explicitly called when I'm deleting them, but I want to make sure that I understand this completely, and that I'm going about it the right way. I'm almost certainly going to scrap this approach and go about things in a more straightforward manner, but I figured that I'd ask just for understanding's sake. I have

move-construct object with placement new

我的未来我决定 提交于 2019-12-12 19:45:25
问题 Is it not UB to move-construct an object via placement new? Let's say I have this code: class Foo { public: Foo() { foo_ = new int; } ~Foo() { delete foo_; } Foo(Foo &&f) { foo_ = std::swap(f.foo_, foo_); } private: int* foo_; } void bar() { void* pMem = malloc(sizeof(Foo)); Foo f1; // move-construct with placement new: new((Foo*)pMem) Foo(std::move(f1)); // f2 in *pMem // now f1 will contain a pointer foo_ of undefined value // when exiting scope f1.~Foo(){} will exhibit UB trying to delete

How to directly read a huge chunk of memory into std::vector?

孤街浪徒 提交于 2019-12-12 08:44:43
问题 I have a huge contiguous array x that I fread from a file. How do I drop this chunk into a std::vector<> ? In other words, I prefer to have the result to be in std::vector<> rather than the array, but I want the resultant C++ code to be as efficient as this plain C-version which drops the chunk right into the array. From searching around, I think I may have to use placement-new in some form, but I'm uncertain about the sequence of calls and ownership issues. Also, do I need to worry about

Finding out the largest size of several objects for placement new

主宰稳场 提交于 2019-12-10 19:06:19
问题 So I'm working on a small ARM embedded system that has pretty limited memory, and no MMU. I have several objects that I need to dynamically allocate for different functions that all inherit from the same superclass, but perform different functions and are likely different sizes. I don't have enough memory available to instantiate them all at startup and leave them in place. So as an example, I might have objects defined sort of like: class Waveform1 : public OutputStream class Waveform2 :

How to create an array while potentially using placement new

被刻印的时光 ゝ 提交于 2019-12-10 14:26:20
问题 I have been working on creating a custom allocator as a fun exercise/practice and I ran into two potentials issues with creating arrays. For a typical call for allocation, I will use malloc and placement new . However, when I go to create an array I am confused on how it should be done. For once, I have noticed at some places that it seems placement new may not be safe for arrays such as here. I am also running into an error of my own while attempting to use placement new for an array. I will

What is the <- symbol in Rust?

*爱你&永不变心* 提交于 2019-12-10 02:22:49
问题 What is the <- operator/expression in Rust? You can find the symbol here. I happened to be looking at a page describing expressions and operations in Rust. I do not program in Rust, so I asked a friend who is pro-Rust what this symbol is but even he doesn't know what it is. 回答1: The <- operator is not part of stable Rust. At least not yet. There is an RFC which proposes syntax involving <- for writing new objects directly to specific places in memory, as an alternative to another RFC, which

std::optional implemented as union vs char[]/aligned_storage

时间秒杀一切 提交于 2019-12-09 09:46:24
问题 While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template <typename T> class optional { // ... private: bool has_value_; aligned_storage<T, /* ... */> storage_; } But then both libstdc++ and libc++ (and Abseil ) implement their optional types like this: template <typename T> class optional { // ... private: struct empty_byte {}; union { empty_byte empty_; T value_; }; bool has_value_; } They look to

c++ placement new in a home made vector container

徘徊边缘 提交于 2019-12-06 02:30:05
There are some questions quite similar around here, but they couldn't help me get my mind around it. Also, I'm giving a full example code, so it might be easier for others to understand. I have made a vector container (couldn't use stl for memory reasons) that used to use only operator= for push_back*, and once I came accross placement new, I decided to introduce an additional "emplace_back" to it**. *(T::operator= is expected to deal with memory management) **(the name is taken from a similar function in std::vector that I've encountered later, the original name I gave it was a mess). I read

Is such assignment a good idea in C++

Deadly 提交于 2019-12-06 01:21:41
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 equal assert(p_n == this); } return *this; } Fred Larson Herb Sutter has addressed this in one of his GotW