placement-new

At what point does the lifetime of a trivial type created by placement-new start?

旧巷老猫 提交于 2019-12-06 01:08:17
问题 During a dive into dynamic memory, it occurred to me it appears contradictory how trivial types begin their lifetime. Consider the snippet void* p = ::operator new(sizeof(int)); // 1 // 2 new (p) int; // 3 When does the int start its lifetime? Only acquires storage, ::operator new is specified to have the effect (from [new.delete.single]) The allocation functions called by a new-expression to allocate size bytes of storage. [...] allocates storage suitably aligned to represent any object of

C++ - overload operator new and provide additional arguments

五迷三道 提交于 2019-12-05 23:52:48
问题 I know you can overload the operator new . When you do, you method gets sent a size_t parameter by default. However, is it possible to send the size_t parameter - as well as additional user-provided parameters, to the overloaded new operator method? For example int a = 5; Monkey* monk = new Monkey(a); Because I want to override new operator like this void* Monkey::operator new(size_t size, int a) { ... } Thanks EDIT: Here's what I a want to accomplish: I have a chunk of virtual memory

What is the meaning of the below sentence in c++ [duplicate]

[亡魂溺海] 提交于 2019-12-05 22:26:04
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents, is it the way of typecasting? or what void someCode() { char memory[sizeof(Fred)]; // Line #1 void* place = memory; // Line #2 Fred* f = new(place) Fred(); // Line #3 // The pointers f and place will be equal ... } This is a typical usage of Placement new . It allows you to allocate memory and then construct objects at that particular memory location. Line #3 essentially just calls the constructor Fred::Fred() . The this pointer in the Fred constructor will be equal to place . The returned pointer f will

Destructor not called after destroying object placement-new'ed

蹲街弑〆低调 提交于 2019-12-05 16:42:58
问题 I had no clue why this doesn't work. The following Function is created by placement new. A function is provided that checks whether it should be destructed, and if so, calls its destructor manually. Here is the testcase where it seems the destructor is never called: /* Represents a function at runtime */ class Function { public: /* Creates an invalid function */ Function():codeptr(0) { } /* Creates a function with the given code pointer */ Function(void *codeptr):codeptr(codeptr) { } /* Frees

Is it legal to use placement new on initialised memory?

混江龙づ霸主 提交于 2019-12-05 07:50:20
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 place of the old one new (&var) Immutable(state1); Assuming that there is no non-trivial destructor to

Undefined behaviour on reinitializing object via placement new on this pointer

巧了我就是萌 提交于 2019-12-05 04:31:24
I saw a presentation on cppcon of Piotr Padlewski saying that the following is undefined behaviour: int test(Base* a){ int sum = 0; sum += a->foo(); sum += a->foo(); return sum; } int Base::foo(){ new (this) Derived; return 1; } Note: Assume sizeof(Base) == sizeof(Derived) and foo is virtual. Obviously this is bad, but I'm interested in WHY it is UB. I do understand the UB on accessing a realloc ed pointer but he says, that this is the same. Related questions: Is `new (this) MyClass();` undefined behaviour after directly calling the destructor? where it says "ok if no exceptions" Is it valid

Is move assignment via destruct+move construct safe?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 03:36:43
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 standard C++11? Only if you never, ever derive a type from this class. If you do, this will turn the

What is the <- symbol in Rust?

本小妞迷上赌 提交于 2019-12-05 02:51:13
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. 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 proposes in . This is a generalisation of the (currently unstable) box syntax, which lets you allocate

C++ strict aliasing when not using pointer returned by placement new

北城以北 提交于 2019-12-04 19:40:50
问题 Can this potentially cause undefined behaviour? uint8_t storage[4]; // We assume storage is properly aligned here. int32_t* intPtr = new((void*)storage) int32_t(4); // I know this is ok: int32_t value1 = *intPtr; *intPtr = 5; // But can one of the following cause UB? int32_t value2 = reinterpret_cast<int32_t*>(storage)[0]; reinterpret_cast<int32_t*>(storage)[0] = 5; char has special rules for strict-aliasing. If I use char instead of uint8_t is it still Undefined Behavior? What else changes?

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

爷,独闯天下 提交于 2019-12-04 05:11:24
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 alignment issues? I am testing for with T = unsigned , but I expect a reasonable solution to work for any