Questions tagged [placement-new]

In C++ placement new is used to construct an object at a particular memory location or to pass additional arguments to an allocation function.

391 questions
10
votes
4 answers

Is this hack to remove aliasing warning UB?

We just upgraded our compiler to gcc 4.6 and now we get some of these warnings. At the moment our codebase is not in a state to be compiled with c++0x and anyway, we don't want to run this in prod (at least not yet) - so I needed a fix to remove…
Nim
  • 33,299
  • 2
  • 62
  • 101
10
votes
1 answer

Using placement new in generic programming

When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitialized_copy: ([uninitialized.copy]) template
L. F.
  • 19,445
  • 8
  • 48
  • 82
10
votes
1 answer

Why there is no std::uninitialized_move_if_noexcept?

C++17 adds std::uninitialized_move, but there is no std::uninitialized_move_if_noexcept that would use std::move_if_noexcept internally. In my opinion, it would be useful, since now, if we want to reallocate, we still need to write something as if…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
10
votes
4 answers

Why isn't it undefined behaviour to destroy an object that was overwritten by placement new?

I'm trying to figure out whether the following is undefined behaviour. I have a feeling it's not UB, but my reading of the standard makes it look like it is UB: #include struct A { A() { std::cout << "1"; } ~A() { std::cout <<…
10
votes
2 answers

Is move assignment via destruct+move construct safe?

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) { …
9
votes
4 answers

Placement new breaks consts and references?

Following the discussion on my answer to this question, apparently: the following code is allowed struct Foo { int x; }; Foo f; Foo & f_ref = f; (&f) -> ~Foo (); new (&f) Foo (); int x = f_ref .x; but the following code is not allowed struct…
spraff
  • 32,570
  • 22
  • 121
  • 229
9
votes
2 answers

Is `new (this) MyClass();` undefined behaviour after directly calling the destructor?

In this question of mine, @DeadMG says that reinitializing a class through the this pointer is undefined behaviour. Is there any mentioning thereof in the standard somewhere? Example: #include class X{ int _i; public: X() : _i(0) {…
Xeo
  • 129,499
  • 52
  • 291
  • 397
9
votes
2 answers

Variation on the type punning theme: in-place trivial construction

I know this is a pretty common subject, but as much as the typical UB is easy to find, I did not find this variant so far. So, I am trying to formally introduce Pixel objects while avoiding an actual copy of the data. Is this valid? struct Pixel { …
9
votes
1 answer

Is new (this) ThisClass() a bad idea?

class FooView final : public Something { ... void refresh() { this->~FooView(); new (this) FooView(); } } I have never seen this idiom, and it seems like it could be really subtle and messy, but I can't actually…
luqui
  • 59,485
  • 12
  • 145
  • 204
9
votes
1 answer

What is the <- symbol in Rust?

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…
FredMan
  • 861
  • 7
  • 19
9
votes
1 answer

False warning for placement new?

I have the following code: #include "type_traits" #include void foo() { std::aligned_storage<10,alignof(long)> storage; new (&storage) int(12); } I have some storage defined (with length 10 bytes), and I am placement newing an int…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
9
votes
5 answers

char array as storage for placement new

Is the following legal C++ with well-defined behaviour? class my_class { ... }; int main() { char storage[sizeof(my_class)]; new ((void *)storage) my_class(); } Or is this problematic because of pointer casting/alignment considerations?
bluescarni
  • 3,937
  • 1
  • 22
  • 33
9
votes
1 answer

Can placement new (expression) throw if the constructor of the object is noexcept?

template struct Obj { // Plain Old Data for T using InternalPod = typename std::aligned_storage::value>::type; InternalPod value_pod_; template Obj(Args&&... args) { // my…
bolov
  • 72,283
  • 15
  • 145
  • 224
9
votes
3 answers

Placement forms of the operator delete functions

In his new book TC++PL4, Stroustrup casts a slightly different light on a once usual practice regarding user-controlled memory allocation and placement new—or, more specifically, regarding the enigmatical "placement delete." In the book's sect.…
thb
  • 13,796
  • 3
  • 40
  • 68
8
votes
1 answer

C++ - overload operator new and provide additional arguments

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…
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199