I l@ve RuBoard Previous Section Next Section

Item 36. Memory Management桺art 2

Difficulty: 6

Are you thinking about doing your own class-specific memory management, or even replacing C++'s global new and delete? First, try this problem on for size.

The following code shows classes that perform their own memory management. Point out as many memory-related errors as possible, and answer the additional questions.

  1. Consider the following code:

    
    
    class B 
    
    
    {
    
    
    public:
    
    
      virtual ~B();
    
    
      void operator delete  ( void*, size_t ) throw();
    
    
      void operator delete[]( void*, size_t ) throw();
    
    
      void f( void*, size_t ) throw();
    
    
    };
    
    
    class D : public B
    
    
    {
    
    
    public:
    
    
      void operator delete  ( void* ) throw();
    
    
      void operator delete[]( void* ) throw();
    
    
    };
    
    
    

    Why do B's operators delete have a second parameter, whereas D's do not? Do you see any way to improve the function declarations?

  2. Continuing with the same piece of code: Which operator delete() is called for each of the following delete expressions? Why, and with what parameters?

    
    
    D* pd1 = new D; 
    
    
    delete pd1;
    
    
    B* pb1 = new D;
    
    
    delete pb1;
    
    
    D* pd2 = new D[10];
    
    
    delete[] pd2;
    
    
    B* pb2 = new D[10];
    
    
    delete[] pb2;
    
    
    
  3. Are the following two assignments legal?

    
    
    typedef void (B::*PMF)(void*, size_t); 
    
    
    PMF p1 = &B::f;
    
    
    PMF p2 = &B::operator delete;
    
    
    
  4. Are there any memory-related errors or issues in the following code?

    
    
    class X 
    
    
    {
    
    
    public:
    
    
      void* operator new( size_t s, int )
    
    
                       throw( bad_alloc )
    
    
      {
    
    
        return ::operator new( s );
    
    
      }
    
    
    };
    
    
    class SharedMemory
    
    
    {
    
    
    public:
    
    
      static void* Allocate( size_t s )
    
    
      {
    
    
        return OsSpecificSharedMemAllocation( s );
    
    
      }
    
    
        static void  Deallocate( void* p, int i = 0 )
    
    
        {
    
    
          OsSpecificSharedMemDeallocation( p, i );
    
    
        }
    
    
      };
    
    
      class Y
    
    
      {
    
    
      public:
    
    
        void* operator new( size_t s,
    
    
                            SharedMemory& m ) throw( bad_alloc )
    
    
        {
    
    
          return m.Allocate( s );
    
    
        }
    
    
        void  operator delete( void* p,
    
    
                               SharedMemory& m,
    
    
                               int i ) throw()
    
    
        {
    
    
           m.Deallocate( p, i );
    
    
        }
    
    
      };
    
    
      void operator delete( void* p ) throw()
    
    
      {
    
    
        SharedMemory::Deallocate( p );
    
    
      }
    
    
      void operator delete( void* p,
    
    
                            std::nothrow_t& ) throw()
    
    
      {
    
    
        SharedMemory::Deallocate( p );
    
    
      }
    
    
    
    I l@ve RuBoard Previous Section Next Section