views:

216

answers:

3

Class-specific version of placement new can be provided even though you can't replace the global one. What scenarios exist where a class should provide its own placement new operator?

Even if my class don't implement placement new the following code works (assuming for abc no operator new is overloaded).

char arr[100];

abc *pt = new(&arr)abc;

So i interpret, there is some default placement new but for class we can provide our own version of operator new, my question is what is the use case for that? What one is supposed to do other then returning the same pointer that is passed? Is there any useful example/scenario that you encountered?

A: 

Straight from the horse's mouth wiki. The section titled 'Use' highlights the need for placement new. This SO thread here might also help

UPDATE: To specifically answer you question; You might use the standard placement new provided by header <new> if you have a pool of memory you want to use for constructing some objects of a class, but don't want to overload operator new for the whole class. In the latter case all the class objects are placed as per the overloaded placement new as defined in the class

Abhay
Abhay, The thread you provided discuss about need of placement new, my question is why one would provide his own implementation instead of using default one?
Learner
@learner: There is nothing like default placement new. The programmer has to provide one and the reasons are stated there. That it is usually done on a class specific basis is a design issue where you will need to be careful with copy-semantics (See C++ Coding Standards - Item 45, By Sutter and Alexandrescu).
Abhay
@Abhay: following code works even if my class donot implement placement new char arr[100]; abc *pt = new(So i interpret, there is some default placement new, right?In class we can provide our own version, my question is what is the use case for that? what one is supposed to do other then returning the same pointer that is passed? Is there any useful example/scenario that you encountered?
Learner
@learner: If you #include<new>, the standard placement new operator is available. But IMHO it must not be called 'default'. It will not be provided without the header. The update may answer your question specifically.
Abhay
A: 

Sounds like a quiz question...

Faster and Leaner Allocation
The most common reason is a lot of small objects that need to be allocated dynamically. A custom allocator for fixed-size objects has much less allocation overhead than a generic allocator, does not suffer from fragmentation, and is typically faster. (Also, when these allocations are removed from the main heap, they don't contribute to main heap fragmentation anymore).

Similary, a non-freeing allocator (where you can allocate multiple objects, but can't free them together, only in conjunction) is the fastest allocation scheme possible, and does not have any overhead (except alignment in a few rare cases). It makes sense if you are building a data structure that you never modify, only delete as a whole.

Other base allocator
Another application is allocating from a different heap than the C++ heap. Maybe the data in the objects needs to be allocated in shared memory for exchange with other processes, or it needs to be passed to a system function that takes ownership and requries the use of a certain allocator. (Note that this requires to implement the same mechanism for all sub-objects, too, there is no generic way to achieve that).

Similary (where I use it) is when you create code on the fly. Nowadays, you need to tell the OS that data on this memory page is allowed to run, but you get this memory in rather large chunks (e.g. 4K). So again, request a page (4K) from the OS with execution rights, then allocate many small objects on top of it - using placement new.

peterchen
@peterchen: I rephrased the question, previously i think it sounded more like use of placement new. :-)
Learner
A: 

i'm not sure that its possible to overload the placement new, only the regular new. i can't think of even a single use for that, since the only possible implementation is just creating a temp object and memcp'ing it to the given memory address - since you're not supposed to allocate any other memory in there, but use the given one.

rmn