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
6
votes
1 answer

Mixing pass-by-reference and pass-by-value to variadic template function valid?

I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...);…
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
5
votes
0 answers

std::launder vs placement-new reachability condition

std::launder has a precondition that all bytes reachable from the would-be-returned pointer are reachable through the passed pointer. My understanding is that this is meant to allow compiler optimizations, so that e.g. struct A { int a[2]; …
user17732522
  • 53,019
  • 2
  • 56
  • 105
5
votes
5 answers

Can I call delete on the pointer which is allocated with the placement new?

Can we call delete on the pointer which is allocated with the placement new? If no then why? Please explain in details. I know that there is no placement delete. But I wonder why just delete opetator can not delete the memory without caring how…
Narek
  • 38,779
  • 79
  • 233
  • 389
5
votes
2 answers

C++ placement new after memset

Suppose there's a struct whose constructor does not initialize all member variables: struct Foo { int x; Foo() {} } If I memset some buffer to 0, use placement new on that buffer to create an instance of Foo, and then read x from that instance,…
Daniel Ricketts
  • 447
  • 2
  • 8
5
votes
2 answers

What does it mean to obtain storage?

[basic.indet] p1 says: When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until…
Krystian S
  • 1,586
  • 7
  • 23
5
votes
2 answers

STL Containers allocation placement new

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…
Kiran
  • 5,478
  • 13
  • 56
  • 84
5
votes
2 answers

Unusual use of new in historical code. What does it mean?

I am just porting some old code: #define NewArrayOnHeap(TYPE, COUNT, HEAP, NEWPTR, ERROR) \ ((*(NEWPTR) = new ( #TYPE "[" #COUNT "]", __alignof(TYPE), (HEAP), &hr, (ERROR)) TYPE[COUNT] ), hr) It looks like the original was supposed to define their…
Martin York
  • 257,169
  • 86
  • 333
  • 562
5
votes
2 answers

Boost shared_ptr with overloaded placement new/delete

I am using boost shared_ptr with my own memory manager like this (stripped down example, I hope there are no errors in it): class MemoryManager { public: /** Allocate some memory.*/ inline void* allocate(size_t nbytes) { return…
Nathan
  • 7,099
  • 14
  • 61
  • 125
5
votes
1 answer

Use of placement-new operator and copy constructor instead of assignment operator

I found a problem while using 3rd party code which cannot be altered. I need to make a copy of object member. I can't do this strictly because one of inner members has private assignment operator. The only solution I found is tricky so I want to ask…
5
votes
1 answer

move-construct object with placement new

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*…
Bogdan Ionitza
  • 331
  • 1
  • 6
  • 14
5
votes
2 answers

construct a vector in range without copying

I have a class that wraps a big array of bytes that are network packets. The class implements a queue and provides (among others) front() function that returns a const vector of bytes that constitute the oldest packet in the queue. class Buffer{ …
Patryk
  • 1,421
  • 8
  • 21
5
votes
1 answer

std::shared_ptr preallocation memory

I want to preallocate memory both for control block and value_type for shared_ptr in one heap request (like std::make_shared), but do not construct immediately any object in it. And when I actually need to construct object use placement new. Is it…
user1641854
5
votes
5 answers

How to determine if object has been placed using placement new

Using the placement new syntax, I should be able to do something like this: char *buffer = new char[sizeof(MyClass)]; //pre-allocated buffer MyClass *my_class = new (buffer) MyClass; //put da class there Now suppose I just do the first line, but…
MattyZ
  • 1,541
  • 4
  • 24
  • 41
5
votes
1 answer

Why can't a qualified type name in an explicit destructor call be parsed correctly?

Consider an example. #include struct S { S() { new (&s) std::string("hi"); } ~S() { // does not compile // s.~std::string(); // compiles using std::string; s.~string(); } …
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
5
votes
4 answers

Placement new and exceptions

The "placement new" operator is declared as such: void* operator new (std::size_t size, void* ptr) noexcept; But while it doesn't involve any actual allocation so bad allocation exceptions are eliminated, it is still possible that the pointer…
user3735658