Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading global operator new/delete in C++

I am trying to overload the global operator new and delete for a performance sensitive application. I have read the concerns described at http://www.informit.com/articles/article.aspx?p=30642&seqNum=3 and the recommendations to use Intel TBB's allocator http://www.intel.com/technology/itj/2007/v11i4/5-foundations/5-memory.htm

Since I am overloading new and delete for the first time, I have a few questions.

Should I include my new header Allocator.h (or Pre.h) containing the overloaded new function in all files containing "new" calls? This is tedious.

Or should I use "gcc -include Allocator.h ..." which includes Allocator.h (before) into each translation unit? I want to keep the code platform independent as much as possible. Do all compilers support something analogous to "gcc -include"?

like image 518
amit kumar Avatar asked Mar 12 '09 17:03

amit kumar


2 Answers

If you want to overload the global operator new and operator delete, you just need to implement it. You don't need to explicitly define it everywhere since it already is defined as part of the language.

Edit: If you want to define an operator new that takes different parameters, then you'll need to #include it everywhere. However you do that is up to you; it's mostly a matter of style.

And don't forget to implement all the variants of global operator new and delete: new, new[], delete, delete[], and the std::nothrow variants.

like image 183
MSN Avatar answered Sep 22 '22 07:09

MSN


If you are using Visual Studio and using precompiled headers, then you can throw the include into there. I think gcc has some form of precompiling headers as well, which would improve your compilation times as well.

like image 33
wchung Avatar answered Sep 22 '22 07:09

wchung