Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading the global operator new

Reply
Thread Tools

overloading the global operator new

 
 
Zachary Turner
Guest
Posts: n/a
 
      12-10-2004
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

 
Reply With Quote
 
 
 
 
Raymond Martineau
Guest
Posts: n/a
 
      12-11-2004
On 10 Dec 2004 13:35:30 -0800, "Zachary Turner" <(E-Mail Removed)>
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
overloading global dereference operator? TuxC0d3 C++ 6 12-06-2005 12:12 AM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
overloading operator<< as global friend funcion Robert Wierschke C++ 3 08-03-2004 04:59 AM
Overloading global new[] array operator in visual c++ 6.0 Tim Milstead C++ 0 04-20-2004 12:50 PM



Advertisments