TITLE: Size argument to X::new() may not be equal to sizeof(X)


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


#include <stddef.h>
#include <assert.h>


class Base {
    public:
	static void* operator new (size_t);
	...
};


void* Base :: operator new (size_t sz)
{
	assert (sz == sizeof(Base)); // ASSERTION FAILS!
	...
}


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


int main ()
{
	Derived* p = new Derived;
	return 0;
}


Moral of the story: do not make any assumptions about the
size passed into class-specific operator new; it might be
larger than you think.
