#include <stdio.h>
#include <new>
static void* myLastNewAllocation;
static void* myLastDelete;
void* operator new(size_t s) throw (std::bad_alloc)
{
myLastNewAllocation = malloc(s);
return myLastNewAllocation;
}
struct Foo {
int bytes[10];
};
__attribute__((weak))
void operator delete(void* p) throw()
{
myLastDelete = p;
::free(p);
}
int main()
{
printf("[BEGIN] operator-new\n");
myLastNewAllocation = NULL;
char* stuff = new char[24];
if ( (void*)stuff != myLastNewAllocation ) {
printf("[FAIL] operator-new system array allocator not redirected through my operator new\n");
return 0;
}
myLastNewAllocation = NULL;
Foo* foo = new Foo();
if ( (void*)foo != myLastNewAllocation ) {
printf("[FAIL] operator-new allocation not redirected though my operator new\n");
return 0;
}
delete foo;
if ( (void*)foo != myLastDelete ) {
printf("[FAIL] operator-new deallocation not redirected though my operator delete\n");
return 0;
}
printf("[PASS] operator-new\n");
return 0;
}