The problem I’m running into is that as far as I know the delete operator should be a static function but sometimes the compiler (VC++) seems to be treating it as dynamic.
Given:
class Base { public: void* operator new(size_t size) { /* allocate from custom heap */ } void operator delete(void *p) { customFree(p, sizeof(Base)); } Base() {} virtual ~Base() {} }; class Derived: public Base { public: void* operator new(size_t size) { /* allocate from custom heap */ } void operator delete(void *p) { customFree(p, sizeof(Derived)); } Derived() {} virtual ~Derived() {} }
What I see happening is that deleting the base pointer will result in call to Derived::opeator delete.
Base *p = new Derived(); delete p; //calls Derived::operator delete
If I don’t define ANY destructors then I get what I expected to happen: Base::operator delete is called. This seems to be happening because the compiler is inserting a function called ‘scalar deleting destructor into the vtable when a destructor is defined. Then that function will call Derived::delete.
So I have to questions: 1) Is this standard behavior? 2) When should I be using
void operator delete( void *, size_t );
vs.
void operator delete( void * );
if the above is standard behavior?
It is certainly Standard Behavior. If the derived class’s operator new was used, its operator delete will also be used (also note even though you do not explicitly tell the compiler those functions are static, they are implicitly declared so). There might be the naughty case where you have an operator new in the derived class, but the corresponding operator delete is in the base class. I think that’s valid, but i would avoid that. Relying on the base operator delete, while defining ones own operator new in the derived class will inevitable cause trouble.
You will get undefined behavior 🙂 Everything can happen, including something you would expect (wrongly). Deleting through a base pointer that points to an object of another type requires a virtual destructor. The implicitly declared destructor is not virtual.
If you want to have the size that was allocated known in the operator delete. I wrote about what it means here: What does the C++ new operator do other than allocation and a ctor call? . If you use (from within your overloaded member operator delete/new) the global operator new & delete to get your memory and release it, or even malloc / free, you don’t need that size information. But it could be useful for logging purposes.