Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use placement new?

As it seems, placement new creates a new object on a preallocated memory, so does it mean that it would take less time? Looks like it's faster then allocating using the old ordinary new. Then, if this is so convenient and faster, why not use placement new all the time?

like image 791
JAN Avatar asked Feb 07 '12 21:02

JAN


People also ask

What is placement new and why would I use it?

Placement new is a variation new operator in C++. Normal new operator does two things : (1) Allocates memory (2) Constructs an object in allocated memory. Placement new allows us to separate above two things. In placement new, we can pass a preallocated memory and construct an object in the passed memory.

In which cases we would need to use placement new?

Use cases. Placement new is used when you do not want operator new to allocate memory (you have pre-allocated it and you want to place the object there), but you do want the object to be constructed.

Does placement New allocate memory?

Because placement new does not allocate memory, you should not use delete to deallocate objects created with the placement syntax. You can only delete the entire memory pool ( delete whole ). In the example, you can keep the memory buffer but destroy the object stored in it by explicitly calling a destructor.

Does vector use placement new?

With std::vector , a memory buffer of the appropriate size is allocated without any constructor calls. Then objects are constructed in place inside this buffer using "placement new".


2 Answers

the normal (nonplacement) new is basically equivalent to doing

T* ptr = static_cast<T*>(malloc(sizeof(T)));
new(ptr) T;

Of course the reality looks a bit different due to errorchecking and such, but the result is more or less the same (through not identical, you can't delete a pointer allocated that way, instead you need to call the destructor explicitely (ptr->~T()) and then release the memory using free).

So placement new should indeed be faster then non placement new, since it doesn't need to allocate the memory. However the problem is that the memory needs to be allocated somewhere. So you have essentially replaced one call to new with a call to placement new and some code for the allocation somewhere (if not why would you use new in the first place?). It should be obvious that this is less convinient and more bug prone.

Now of course you can write a faster allocation method, but for that you typically need to do some sort of tradeoff. It's not going to be easy to write a allocator which is faster without either using more memory (extra data for faster identification of free blocks) or making it very specific (writing fast allocation of a single objectsize is much easier then a general one). In the end it is typically not worth the effort (for scenarious where it is worth the effort it has likely already been done, so you could use an existing allocator (which likely uses placement new internally)).

There are of course uses for placement new (sometimes you do have the memory preallocated), but that is simply not the common case

like image 180
Grizzly Avatar answered Oct 01 '22 01:10

Grizzly


It is simply unnecessary for most programs as their usage patterns don't make it necessary. It makes no difference for programs that don't use the heap as much and it is hard to get right (better than your operating system, that is). Also can you only gain so much by optimizing your allocation. For the most part any algorithmic optimization is going to result in a much larger over-all speed-up. A lot of the guarantees that a customized allocator can offer (guaranteed time limits for allocation through pre-allocated memory, low memory fragmentation) are often not needed.

There are definitely programs that can benefit from doing memory-managment themselves, they are just hard to identify. After you have found that memory-allocation is actually a bottle-neck it is even harder to find a better allocation scheme. When all that is done, it is still not often worth the hassle.

like image 42
pmr Avatar answered Oct 01 '22 02:10

pmr