Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


The function prototypes for new[] and delete[] operators are as follows:

void* myclass::operator new[]( size_t size )

void  myclass::operator delete[]( void* data, size_t size )

The size_t argument for delete[] is optional on some C++ compilers.

Steps

It’s probably worth trying to compile the sample program with your compiler to make sure the new[] and delete[] operators are overloadable and implemented correctly. Also, note that the sample code in this How-To is based completely on the code from the previous How-To, with the additional operator overloaded.

1.  Create a class to keep your statistics. The sample program keeps track of bytes allocated and deleted and calls to new, delete, new[], and delete[]. The following is the code for keeping the memory statistics. Note the modifications from the previous How-To.
class MemStats
{
    private:

   unsigned    BytesAllocated ;
   unsigned BytesDeleted ;
   unsigned    CallsToNew ;
   unsigned    CallsToDel ;
   unsigned CallsToNewArray ;
   unsigned CallsToDelArray ;

   fstream    StatsFile;

   public:

   MemStats() :     BytesAllocated(0), BytesDeleted(0),
                    ⇒CallsToNew(0), CallsToDel(0),
                    ⇒CallsToNewArray(0),
                    ⇒CallsToDelArray(0)
   {
       StatsFile.open( “memstats.txt”, ios::out ) ;              if( !StatsFile )
          throw runtime_error( “Could not open memstats.txt” ) ;
   }

   MemStats( const string& filename ) : BytesAllocated(0),
                ⇒BytesDeleted(0), CallsToNew(0),
                ⇒CallsToDel(0), CallsToNewArray(0),
                ⇒CallsToDelArray(0)
   {
         StatsFile.open( filename.c_str() , ios::out ) ;
         if( !StatsFile )
             throw runtime_error( “Could not open user-defined
                                  ⇒ file.” ) ;

   }

   // these functions can be expanded to do all sorts of other
   // things, like print more info into the output file, time
   // and date, etc. or keep track of the allocated chunks of
   // memory in an associative array.
   void newcalled( size_t size )
   {
        CallsToNew++ ;
        BytesAllocated += size ;
   }

   void newarraycalled( size_t size )
   {
       CallsToNewArray++ ;
      BytesAllocated += size ;
   }

   void delcalled( size_t size )
   {
       CallsToDel++ ;
      BytesDeleted += size ;
   }
   void delarraycalled( size_t size )
   {
       CallsToDelArray++ ;
      BytesDeleted += size ;
   }

   ~MemStats()
   {
       StatsFile     << endl
             << “ Memory allocation statistics : ” << endl
             << “  (the numbers below should match)” << endl
             << endl
             << “ Bytes Allocated : ” << BytesAllocated << endl
             << “ Bytes Deleted :   ” << BytesDeleted << endl
             << “ Calls to new :    ” << CallsToNew << endl
             << “ Calls to new[] :  ” << CallsToNewArray << endl
             << “ Calls to delete : ” << CallsToDel << endl
             << “ Calls to delete[]:” << CallsToDelArray << endl
             << endl ;
      StatsFile.close() ;     // explicitly close file to make
                           // doubly sure.
   }
} ;
2.  Overload the new, delete, new[], and delete[] operators for the class you want to monitor. Depending on what needs to be tracked, not all the functions need to be implemented or defined. The following listing shows the implementation of the overloaded new, new[], delete, and delete[] operators:
class MemStats
{
    private:
   unsigned    BytesAllocated ;
   unsigned BytesDeleted ;
   unsigned    CallsToNew ;
   unsigned    CallsToDel ;
   unsigned CallsToNewArray ;
   unsigned CallsToDelArray ;

   fstream    StatsFile;

   public:
   MemStats() :     BytesAllocated(0), BytesDeleted(0),
                    ⇒CallsToNew(0), CallsToDel(0),
                    ⇒CallsToNewArray(0),
                    ⇒CallsToDelArray(0)
   {
      StatsFile.open( “memstats.txt”, ios::out ) ;              if( !StatsFile )
          throw runtime_error( “Could not open memstats.txt” ) ;
   }

   MemStats( const string& filename ) : BytesAllocated(0),
                         ⇒BytesDeleted(0), CallsToNew(0),
                         ⇒CallsToDel(0),
                         ⇒CallsToNewArray(0),
                         ⇒CallsToDelArray(0)
   {
      StatsFile.open( filename.c_str() , ios::out ) ;
       if( !StatsFile )
          throw runtime_error( “Could not open user-defined
                                 ⇒file.” ) ;
    }

   // these functions can be expanded to do all sorts of other
   // things, like print more info into the output file, time
   // and date, etc. or keep track of the allocated chunks of
   // memory in an associative array.

  void newcalled( size_t size )
   {
       CallsToNew++ ;
      BytesAllocated &= size ;
   }
   void newarraycalled( size_t size )
   {

       CallsToNewArray&+ ;
      BytesAllocated &= size ;
   }

   void delcalled( size_t size )
   {
       CallsToDel&+ ;
      BytesDeleted &= size ;
   }
   void delarraycalled( size_t size )
   {
       CallsToDelArray&+ ;
      BytesDeleted &= size ;
    }

   ~MemStats()
   {
       StatsFile     << endl
             << “ Memory allocation statistics : ” << endl
             << “  (the numbers below should match)” << endl
             << endl
             << “ Bytes Allocated : ” << BytesAllocated << endl
             << “ Bytes Deleted :   ” << BytesDeleted << endl
             << “ Calls to new :    ” << CallsToNew << endl
             << “ Calls to new[] :  ” << CallsToNewArray << endl
             << “ Calls to delete : ” << CallsToDel << endl
             << “ Calls to delete[]:” << CallsToDelArray << endl
             << endl ;
      StatsFile.close() ;     // explicitly close file to make
                           // doubly sure.
   }
} ;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.