Home > Fresh News > new and delete

new and delete

January 14th, 2009 Leave a comment Go to comments
// Example 1
// Allocation
T* pv = (T*)malloc( sizeof(T) );
// Construction
::new( pv ) T( /*arg-list*/ );
...
// Destruction
pv->T::~T();
// Deallocation
free( pv );

// Example 2
class Blanks
{
public:
  Blanks(){}
  void* operator new( size_t stAllocateBlock, int chInit )
  {
    void* pvTemp = malloc( stAllocateBlock );
    if( pvTemp != 0 )
    {
      memset( pvTemp, chInit, stAllocateBlock );
    }
    return pvTemp;
  }
};
// For discrete objects of type Blanks, the global operator new function
// is hidden. Therefore, the following code allocates an object of type
// Blanks and initializes it to 0xa5
Blanks *a5 = new(0xa5) Blanks;
bool b = a5 != 0;
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.