fork download
  1. #include <iostream>
  2. #include <new>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. struct Test
  8. {
  9. Test(int d) : data(d) { cerr << "Test::Test" << endl; }
  10. ~Test() { cerr << "Test::~Test" << endl; }
  11.  
  12. /* static */ void* operator new(size_t sz);
  13. /* static */ void operator delete(void* p) noexcept;
  14. int data;
  15. };
  16.  
  17. // member new and delete
  18. void* Test::operator new(size_t sz)
  19. {
  20. cerr << "Test::new" << endl;
  21. return ::operator new(sz);
  22. }
  23. void Test::operator delete(void* p) noexcept
  24. {
  25. cerr << "Test::delete" << endl;
  26. ::operator delete(p);
  27. }
  28.  
  29. // global new and delete
  30. void* operator new(size_t sz)
  31. {
  32. cerr << "::new" << endl;
  33. return malloc(sz);
  34. }
  35. void operator delete(void* p) noexcept
  36. {
  37. cerr << "::delete" << endl;
  38. free(p);
  39. }
  40.  
  41. int main()
  42. {
  43. Test *t = new Test(42);
  44. /* ... */
  45. delete t;
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout #stderr 0s 4352KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Test::new
::new
Test::Test
Test::~Test
Test::delete
::delete