#include <iostream>
#include <string>
#include <new>
#include <cstdlib>
//#include <stdlib.h>
void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "::new:" << p << std::endl; return p; }
void operator delete (void *p) throw () { std::cout << "::delete:" << p << std::endl; free(p); }
struct base {
base() { }
virtual ~base() { }
void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "base::new:" << p << std::endl; return p; }
void operator delete (void *p) throw () { std::cout << "base::delete:" << p << std::endl; free(p); }
};
struct derived : base {
std::string s;
derived(){ }
~derived(){ }
void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "derived::new:" << p << std::endl; return p; }
void operator delete (void *p) throw () { std::cout << "delete::delete:" << p << std::endl; free(p); }
};
int main()
{
derived* pD= new derived;
pD->s = "xyz";
base* pB = pD;
delete pB;
return 0;
}
/* end */
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bmV3PgojaW5jbHVkZSA8Y3N0ZGxpYj4KCi8vI2luY2x1ZGUgPHN0ZGxpYi5oPgoKdm9pZCAqb3BlcmF0b3IgbmV3KHNpemVfdCBzKSB0aHJvdyAoc3RkOjpiYWRfYWxsb2MpIHsgdm9pZCAqcCA9IG1hbGxvYyhzKTsgc3RkOjpjb3V0IDw8ICI6Om5ldzoiIDw8IHAgPDwgc3RkOjplbmRsOyByZXR1cm4gcDsgfQp2b2lkIG9wZXJhdG9yIGRlbGV0ZSAodm9pZCAqcCkgdGhyb3cgKCkgeyBzdGQ6OmNvdXQgPDwgIjo6ZGVsZXRlOiIgPDwgcCA8PCBzdGQ6OmVuZGw7IGZyZWUocCk7IH0KCnN0cnVjdCBiYXNlIHsKICBiYXNlKCkgeyB9CiAgdmlydHVhbCB+YmFzZSgpIHsgfQogIHZvaWQgKm9wZXJhdG9yIG5ldyhzaXplX3QgcykgdGhyb3cgKHN0ZDo6YmFkX2FsbG9jKSB7IHZvaWQgKnAgPSBtY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