9

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?

DigviJay Patil
  • 986
  • 14
  • 31
BigSandwich
  • 2,768
  • 2
  • 22
  • 26

2 Answers2

8

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: 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.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Disregard the comment I just deleted. So is there any benefit to using void operator delete( void *, size_t ); over sizeof(classname) ? – BigSandwich Jan 02 '09 at 23:05
  • 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 Jan 02 '09 at 23:18
  • 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 Jan 02 '09 at 23:19
  • @JohannesSchaub-litb Isn't this all about the concept of scopes with respect to classes? I mean when delete is called, it looks for an appropriate version in the derived class scope and since it finds one, that version is called. This is the inner meaning of what you said "If the derived class's operator new was used, its operator delete will also be used". Please correct me if I am wrong. – ubuntugod Feb 23 '16 at 07:27
6

(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)