Board index » cppbuilder » overloading operator new

overloading operator new


2005-12-13 08:12:51 AM
cppbuilder5
Hi,
I would like to overload the new operator in BCB to track memory
allocations etc, and stuff like that, is it possible?
thanks
Simon
 
 

Re:overloading operator new

Simon Guertin wrote:
Quote
Hi,
I would like to overload the new operator in BCB to track memory
allocations etc, and stuff like that, is it possible?

thanks

Simon
Hi Simon,
Yes it is possible to overload the new operator. We do it like this:
/*** Code snippet ***/
* operator new( size_t ANewSize, const char *FileName, int LineNum )
{
void *pMemBlock = malloc( ANewSize );
if( NULL == pMemBlock )
{
printf( "Memory allocation error at %s:%d\n", FileName, LineNum );
}
return pMemBlock;
}
//---------------------------------------------------------------------------
void * operator new[]( size_t ANewSize, const char *FileName, int LineNum )
{
void *pMemBlock = malloc( ANewSize );
if( NULL == pMemBlock )
{
printf( "Memory allocation error at %s:%d\n", FileName, LineNum );
}
return pMemBlock;
}
//---------------------------------------------------------------------------
void operator delete( void *ApMemBlock )
{
if( NULL != ApMemBlock )
{
free( ApMemBlock );
}
}
//---------------------------------------------------------------------------
void operator delete[]( void *ApMemBlock )
{
if( NULL != ApMemBlock )
{
free( ApMemBlock );
}
}
/*** Code snippet ***/
Remember there are two different new operators: the new object and new array
operators, both are shown above. And because we allocate memory with malloc, we
need to overload the delete operator as well to deallocate it properly.
to call these new operators you would have to write:
int *pArray = new( __FILE__, __LINE__ ) int[ SOME_SIZE ];
or
CSomeClass *pClass = new( __FILE__, __LINE__ ) CSomeClass;
Because these new operators have arguments, the functions we have defined will
be called. Ofcourse you can define other arguments, but these are very helpful
to identify which new operator was faulty. The printf statement is just here for
simplicity, but it would be better to throw an exception with the given information.
The drawback however is, that all new operators throughout your program have to
be modified with the same arguments, otherwise the compiler will still call the
default operators.
It is also possible to define a new operator as a class member. The BCB Help
should provide you with enough information about this.
Hope this was helpful.
Cheers,
Wiljo.
 

Re:overloading operator new

Wiljo wrote:
Quote
Hi Simon,

Yes it is possible to overload the new operator. We do it like this:

/*** Code snippet ***/

* operator new( size_t ANewSize, const char *FileName, int LineNum )
This ofcourse has to start with a void:
void * operator new( size_t ANewSize, const char *FileName, int LineNum )
Sorry for the mistake. The copy and paste block was a little too small.
Quote
Cheers,

Wiljo.
 

{smallsort}

Re:overloading operator new

Wiljo wrote:
Quote
Wiljo wrote:

>Hi Simon,
>
>Yes it is possible to overload the new operator. We do it like this:
>
>/*** Code snippet ***/
>
>* operator new( size_t ANewSize, const char *FileName, int LineNum )


This ofcourse has to start with a void:

void * operator new( size_t ANewSize, const char *FileName, int LineNum )

Sorry for the mistake. The copy and paste block was a little too small.

>Cheers,
>
>Wiljo.
Thanks, this is what I was looking for!