Overloading global operator new

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I am wanting to write a memory allocation statistic gathering wrapper and
here is a basic bit of code that I wrote to back it up:

class CMemoryManager
{
public:
static void* operator new(size_t AllocationSize)
{
++m_NumberOfAllocations;

return ::operator new(AllocationSize);
}

static int m_NumberOfAllocations;
};

int CMemoryManager::NumberOfAllocations = 0;

void* operator new(size_t AllocationSize)
{
return CMemoryManager::operator new(AllocationSize);
}

int main()
{
int* pInt = new int;

return 0;
}

now this all compiles just fine, but my call to ::operator new inside
CMemoryManager resolves back to my overridden operator new rather than the
intrinsic global new.

I really don't want to force client code to use some specially named version
of new or something with some bogo parameters but it is looking like that is
impossible unless I convert my memory manager to use malloc internally or
spomething. Any ideas?
 
Alex said:
I really don't want to force client code to use some specially named
version of new or something with some bogo parameters but it is
looking like that is impossible unless I convert my memory manager to
use malloc internally or spomething. Any ideas?

Use malloc internally - that's what the original version of the global
operator new does. Be sure to provide a new version of global operator
delete as well.

-cd
 
Back
Top