TITLE: class specific new and delete [ This picks up on a discussion of operator new and operator delete. If you are not at all familiar with this, you should read the text in Scott Meyers book. I missed part of the original discussion. Hopefully there is enough context to get back in sync. - adc ] RESPONSE: kanze@us-es.sel.de (James Kanze) [...] can assume no overloading. This allows the use of a fixed size allocator (which will often be a magnitude or two faster than the general operator new). Note that a class specific operator new generally requires a class specific operator delete, and that this operator delete can be declared to take two parameters, the second of which is the size of the object being deleted. So something like the following can be used safely even if some derivation is taking place: void* MyClass::operator new( size_t size ) { if ( size != sizeof( MyClass ) ) return ::operator new( size ) ; else // Use fixed size allocator. } void MyClass::operator delete( void* p , size_t size ) { if ( size != sizeof( MyClass ) ) return ::operator delete( p ) ; else // Use free for fixed size allocator. } [ Note that the test against the sizeof() is needed since the derived classes of MyClass would use the same new/delete and could be larger. -adc ] RESPONSE: Dietmar Kuehl BTW: This is basically the version found in 'Effective C++' by S.Meyers (J.Kanze was unsure about this). However, Mr.Meyers uses (erroneously) '::new char[size]' and '::delete (char*)ptr' instead of 'operator new(size)' and 'operator delete(ptr)' (at least in the German version; I don't have access to the original one...). The subtle difference between 'new char[size]' and 'operator new(size)' is that the latter is guaranteed to be aligned correctly for all types. RESPONSE:smeyers@netcom.com (Scott Meyers), Nov 14 95 He's right. Furthermore, my use (in "Effective C++") of an array of chars as raw storage is now quite properly viewed as a conceptual hack. The proper approach is to call operator new directly, as both Kanze and Kuehl point out. For what it's worth, what follows is the code I currently show in my presentations on C++ corresponding to the fixed-size allocator for the Airplane handle class I show in Effective C++, Item 10. This represents my current thinking on the subject: class Airplane { private: union { // the union idea is from Copelien's book AirplaneRep *rep; // for objects in use Airplane *next; // for objects on free list }; const static int BLOCK_SIZE = 512; static Airplane *headOfFreeList; public: void * operator new(size_t size); void operator delete(void *deadObject, size_t size); ... }; const int Airplane::BLOCK_SIZE; Airplane *Airplane::headOfFreeList = 0; void * Airplane::operator new(size_t size) { if (size != sizeof(Airplane)) return ::operator new(size); Airplane *p = headOfFreeList; // if p is valid, just move the list head to the // next element in the free list if (p) headOfFreeList = p->next; else { // allocate a big block of memory Airplane *newBlock = static_cast(::operator new( BLOCK_SIZE * sizeof(Airplane))); // make sure we got one if (newBlock == 0) return 0; // link the objects together; skip the zeroth // element, because we'll return that for (int i = 1; i < BLOCK_SIZE-1; i++) newBlock[i].next = &newBlock[i+1]; // terminate the linked list with a null pointer newBlock[BLOCK_SIZE-1].next = 0; // set p to front of list, headOfFreeList to // chunk immediately following p = newBlock; headOfFreeList = &newBlock[1]; } return p; } // operator delete is passed a memory chunk, which, // if it's the right size, is just added to the // front of the list of free chunks void Airplane::operator delete(void *deadObject, size_t size) { if (deadObject == 0) return; // send objects of "wrong" size to ::op delete if (size != sizeof(Airplane)) { ::operator delete(deadObject); return; } Airplane *carcass = static_cast(deadObject); carcass->next = headOfFreeList; headOfFreeList = carcass; } The code in the book won't get updated until I write a second edition, which, in view of my work on "More Effective C++" (to be out in January), I'm currently not even contemplating.