overloading the global operator new

Z

Zachary Turner

I'm having trouble overloading the global operator new and delete. I
want to do this for debugging purposes to trace allocations and
deallocations. What I have is

#ifdef _DEBUG
// User-defined operator new.
void *operator new( size_t stAllocateBlock )
{
static fInOpNew = 0; // Guard flag.

if ( fLogMemory && !fInOpNew )
{
fInOpNew = 1;
clog << "Memory block " << ++cBlocksAllocated
<< " allocated for " << stAllocateBlock
<< " bytes\n";
fInOpNew = 0;
}

return malloc( stAllocateBlock );
}

// User-defined operator delete.
void operator delete( void *pvMem )
{
static fInOpDelete = 0; // Guard flag.
if( fLogMemory && !fInOpDelete )
{
fInOpDelete = 1;
clog << "Memory block " << cBlocksAllocated--
<< " deallocated\n";
fInOpDelete = 0;
}

free( pvMem );
}
#endif

(copied directly from microsoft help file just to make sure there's no
syntax errors preventing compilation)

I put this in a regular C++ source file but I get compilation errors.

c:\develop\BrowseBuddy\Memory.cpp(7) : error C2084: function 'void
*operator new(size_t)' already has a body
predefined C++ types (compiler internal)(20) : see previous
definition of 'new'


I'm using VC++ 7 compiler. Anybody have any idea where I might be
going wrong?

Thanks
 
R

Raymond Martineau

I'm having trouble overloading the global operator new and delete. I
want to do this for debugging purposes to trace allocations and
deallocations. What I have is

#ifdef _DEBUG
// User-defined operator new.
void *operator new( size_t stAllocateBlock )

You might not exactly get what you want, but you could try declaring the
procedure as static, and including the piece of code in all files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top