| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Zachary Turner" |
| Date: |
10 Dec 2004 03:35:30 PM |
| Object: |
overloading the global operator new |
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
.
|
|
| User: "Raymond Martineau" |
|
| Title: Re: overloading the global operator new |
10 Dec 2004 09:52:25 PM |
|
|
On 10 Dec 2004 13:35:30 -0800, "Zachary Turner" <divisortheory@gmail.com>
wrote:
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.
.
|
|
|
|

|
Related Articles |
|
|