#ifndef XGMEM_H
#define XGMEM_H
// Visual C++ fix of operator new
//
// Global overwrite of new, delete
//
// On some systems even malloc throws the std::bad_alloc exception
// Could override the default handler and then do your own handler
#include // for malloc, free
#include
using std::cout;
using std::endl;
#include // for std::bad_alloc
using std::bad_alloc;
#include // for size_t
void* operator new (size_t size) {
cout << "Attempting allocating " << size << " bytes" << endl;
void *p = malloc(size);
cout << "Allocating memory of size " << size << " bytes" << endl;
if (p==0) // did malloc succeed?
cout << "Memory exception thrown for " << size << " bytes" << endl;
throw std::bad_alloc(); // ANSI/ISO compliant behavior
return p;
}
void operator delete (void *p) {
free(p);
}
#endif XGMEM_H