C++ – When do you overload operator new?

cmemory-managementnew-operatorobjectoperators

Possible Duplicate:
Any reason to overload global new and delete?

In what cases does it make perfect sense to overload operator new?

I heard you do it in a class that is very often allocated using new. Can you give an example?

And are there other cases where you would want to overload operator new?

Update: Thanks for all the answers so far. Could someone give a short code example? That's what I meant when I asked about an example above. Something like: Here is a small toy class, and here is a working operator new for that class.

Best Answer

Some reasons to overload per class
1. Instrumentation i.e tracking allocs, audit trails on caller such as file,line,stack.
2. Optimised allocation routines i.e memory pools ,fixed block allocators.
3. Specialised allocators i.e contiguous allocator for 'allocate once on startup' objects - frequently used in games programming for memory that needs to persist for the whole game etc.
4. Alignment. This is a big one on most non-desktop systems especially in games consoles where you frequently need to allocate on e.g. 16 byte boundaries
5. Specialised memory stores ( again mostly non-desktop systems ) such as non-cacheable memory or non-local memory