#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
struct Test
{
Test(int d) : data(d) { cerr << "Test::Test" << endl; }
~Test() { cerr << "Test::~Test" << endl; }
/* static */ void* operator new(size_t sz);
/* static */ void operator delete(void* p) noexcept;
int data;
};
// member new and delete
void* Test::operator new(size_t sz)
{
cerr << "Test::new" << endl;
return ::operator new(sz);
}
void Test::operator delete(void* p) noexcept
{
cerr << "Test::delete" << endl;
::operator delete(p);
}
// global new and delete
void* operator new(size_t sz)
{
cerr << "::new" << endl;
return malloc(sz);
}
void operator delete(void* p) noexcept
{
cerr << "::delete" << endl;
free(p);
}
int main()
{
Test *t = new Test(42);
/* ... */
delete t;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bmV3PgojaW5jbHVkZSA8Y3N0ZGxpYj4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgVGVzdAp7CiAgVGVzdChpbnQgZCkgOiBkYXRhKGQpIHsgY2VyciA8PCAiVGVzdDo6VGVzdCIgIDw8IGVuZGw7IH0KICB+VGVzdCgpICAgICAgICAgICAgICAgIHsgY2VyciA8PCAiVGVzdDo6flRlc3QiIDw8IGVuZGw7IH0KCiAgLyogc3RhdGljICovIHZvaWQqIG9wZXJhdG9yIG5ldyhzaXplX3Qgc3opOwogIC8qIHN0YXRpYyAqLyB2b2lkICBvcGVyYXRvciBkZWxldGUodm9pZCogcCkgbm9leGNlcHQ7CiAgaW50IGRhdGE7Cn07CgovLyBtZW1iZXIgbmV3IGFuZCBkZWxldGUKdm9pZCogVGVzdDo6b3BlcmF0b3IgbmV3KHNpemVfdCBzeikKewogIGNlcnIgPDwgIlRlc3Q6Om5ldyIgPDwgZW5kbDsKICByZXR1cm4gOjpvcGVyYXRvciBuZXcoc3opOwp9CnZvaWQgVGVzdDo6b3BlcmF0b3IgZGVsZXRlKHZvaWQqIHApIG5vZXhjZXB0CnsKICBjZXJyIDw8ICJUZXN0OjpkZWxldGUiI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