fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <new>
  4. #include <cstdlib>
  5.  
  6. //#include <stdlib.h>
  7.  
  8. void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "::new:" << p << std::endl; return p; }
  9. void operator delete (void *p) throw () { std::cout << "::delete:" << p << std::endl; free(p); }
  10.  
  11. struct base {
  12. base() { }
  13. ~base() { }
  14. void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "base::new:" << p << std::endl; return p; }
  15. void operator delete (void *p) throw () { std::cout << "base::delete:" << p << std::endl; free(p); }
  16. };
  17.  
  18. struct derived : base {
  19. std::string s;
  20. derived(){ }
  21. ~derived(){ }
  22. void *operator new(size_t s) throw (std::bad_alloc) { void *p = malloc(s); std::cout << "derived::new:" << p << std::endl; return p; }
  23. void operator delete (void *p) throw () { std::cout << "delete::delete:" << p << std::endl; free(p); }
  24. };
  25.  
  26. int main()
  27. {
  28. derived* pD= new derived;
  29. pD->s = "xyz";
  30. base* pB = pD;
  31. delete pB;
  32. return 0;
  33. }
  34. /* end */
  35.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
derived::new:0x8f62008
::new:0x8f62018
base::delete:0x8f62008