#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() { }
~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 */
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bmV3PgojaW5jbHVkZSA8Y3N0ZGxpYj4KCi8vI2luY2x1ZGUgPHN0ZGxpYi5oPgoKdm9pZCAqb3BlcmF0b3IgbmV3KHNpemVfdCBzKSB0aHJvdyAoc3RkOjpiYWRfYWxsb2MpIHsgdm9pZCAqcCA9IG1hbGxvYyhzKTsgc3RkOjpjb3V0IDw8ICI6Om5ldzoiIDw8IHAgPDwgc3RkOjplbmRsOyByZXR1cm4gcDsgfQp2b2lkIG9wZXJhdG9yIGRlbGV0ZSAodm9pZCAqcCkgdGhyb3cgKCkgeyBzdGQ6OmNvdXQgPDwgIjo6ZGVsZXRlOiIgPDwgcCA8PCBzdGQ6OmVuZGw7IGZyZWUocCk7IH0KCnN0cnVjdCBiYXNlIHsKICBiYXNlKCkgeyB9CiAgfmJhc2UoKSB7IH0KICB2b2lkICpvcGVyYXRvciBuZXcoc2l6ZV90IHMpIHRocm93IChzdGQ6OmJhZF9hbGxvYykgeyB2b2lkICpwID0gbWFsbG9jKHMpOyBzdGQ6O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