tags:

views:

667

answers:

2

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?

+3  A: 

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.

If I don't define ANY destructors then I get what I expected to happen:

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.

When should I be using void operator delete( void *, size_t );

If you want to have the size that was allocated known in the operator delete. I wrote about what it means here: http://stackoverflow.com/questions/377178/how-does-the-standard-new-operator-work-in-c#390585 . 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.

Johannes Schaub - litb
Disregard the comment I just deleted. So is there any benefit to using void operator delete( void *, size_t ); over sizeof(classname) ?
BigSandwich
Ah, never mind. I guess new() can allocate any size, so sizeof(classname) could give you an incorrect size. In my case; however, I think they are equivalent. Thanks.
BigSandwich
if you don't use the array form, it has to exactly allocate sizeof(T). so the size_t of your delete should always be exactly sizeof(T) *unless* you actually delete the memory of a derived class. (which happens if the derived class doesn't have an operator delete)
Johannes Schaub - litb
+2  A: 

(hey, I should post first and look up later :) )

Here are the relevant excerpts from the Standard:

1 The delete-expression operator destroys a most derived object (intro.object) or array created by a new-expression. delete-expression: ::opt delete cast-expression ::opt delete [ ] cast-expression The first alternative is for non-array objects, and the second is for arrays. The operand shall have a pointer type, or a class type having a single conversion function (class.conv.fct) to a pointer type. The result has type void.

3 In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand's dynamic type and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.19)

Arkadiy
"hey, I should post first and look up later " certainly a good idea :p you get 5 mins to correct wrong things before the "edited" label appears :DD
Johannes Schaub - litb