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
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