views:

168

answers:

4

Hi,

I know Microsoft themselves used to recommend overriding operator new with calls to HeapCreate() and HeapAlloc(), but that was a while ago. See KB139638 for more details.

Would i still benefit from overriding new/delete on Win32? What would be the recommended implementation?

TIA.

+3  A: 

Unless you're doing a very memory intensive program (in which it anyway would be more effective to overload allocators in specific places), the main benefit of overloading the new and delete operators would be to track allocations and deallocations for debugging and profiling purposes.

Kornel Kisielewicz
just like DEBUG_NEW. I get it.
djeidot
+1  A: 

Overloading global new/delete is a Bad Idea (TM) for most cases.

Class level memory management (via overloading) should be used only when required. Profile your application and see if the bottleneck is with heap based allocations/deallocations. Further, things get more complicated if your class can/will be derived from later on (places an extra burden on the implementor since new/delete are static members).

Try passing in an allocator (with strategies, so you can test which suits your needs the best) similar to what the STL does.

dirkgently
+6  A: 

The article says you can do it, not that you should. The code in it is so badly written it isn't funny, and it is not thread safe. In general, the implementation supplied new and delete will work well for all general programming needs. You should only consider re-implementing them if you have identified a specific problem that such a re-implementation would solve.

anon
In my experience the code in every MSDN sample is horribly written, which makes me chuckle. Then I see an exact copy of that sample is production code over and over, which makes me want to go on a homicidal rampage.
John Dibling
+1  A: 

Do you have a reason to do so? No? Don't do it then; the chances of not making life extremely painful for yourself are slim.

Have you got an app that you've profiled and determined that allocation / deallocation of something is a bottleneck? Because until you've done that, I wouldn't worry about it.

This reaks of premature optomisation to me :-)

Jon Cage