trantran 0 Junior Poster in Training

I want to filter the global operator new and operator delete.
For example, this could be used to count the number of times they are called and check at the end of a program to see if they match for memory leak detection or other purposes.
I do know perfectly how to *replace* global operator new and operator delete, it is somewhat complicated as you need to set new handlers, etc. and the point is that it would be much simpler sometimes to just filter it.
So the question is not *how* to overload or replace it, but how to *filter* it.
A way to do it would be for example something like this:

int count;
void * operator new(size_t s){
++count;
return ::operator new(s);  // WON'T WORK --> result in recursive call to this same function
}

The problem is how do you call the *original* operator new (before you overloaded it) without having for effect to just recall your own operator new function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.