TITLE: Size argument to X::delete() may be surprising


[ Material adapted from "C++ Gotchas", tutorial notes by
  Tom Cargil, p. 50 ]


#include <stddef.h>
#include <iostream.h>

// .......................................................

class Base {
	int filler;
    public:
	void* operator new (size_t);
	void  operator delete (void*, size_t);
	...
};

void* Base :: operator new (size_t sz)
{
	cout << "new(" << sz << ")" << endl;
	...
}

void Base :: operator delete (void* p, size_t sz)
{
	cout << "delete(*," << sz << ")" << endl;
	...
}

// .......................................................

class Derived : public Base {
	int moreFiller;
	...
};

// .......................................................

int main ()
{
	Base* b = new Derived;
	delete b;

	return 0;
}


The output of this program is

new(8)
delete(*,4)

If you add a virtual destructor to the Base class
the new output is

new(12)
delete(*,12)
