fork download
  1. #include <iostream>
  2. #include <new>
  3. using namespace std;
  4.  
  5. class Data {
  6. public:
  7. Data(int first) {
  8. data[0] = first;
  9. cout<<"Constructor 1 " <<first;
  10. }
  11.  
  12. Data(int first, int second) {
  13. data[0] = first;
  14. data[1] = second;
  15. cout<<"Constructor 2 " <<first<< ";"<<second;
  16. }
  17.  
  18. void* operator new(size_t x);
  19.  
  20. void* operator new(size_t x, const std::nothrow_t& nothrow_value) throw();
  21.  
  22. void operator delete(void* ptr);
  23.  
  24. private:
  25. int data[10];
  26. };
  27.  
  28. //class DataTag;
  29. class DumbClass{};
  30.  
  31. //typedef boost::singleton_pool<DataTag, sizeof(Data)> DataPool;
  32.  
  33. void* Data::operator new(size_t x) {
  34. cout<<"new 1 operator called";
  35. void* mem = malloc(x);
  36. return mem;
  37. /*void* ptr = DataPool::malloc();
  38.   if (ptr) {
  39.   return ptr;
  40.   } else {
  41.   throw std::bad_alloc();
  42.   }*/
  43. }
  44.  
  45. void* Data::operator new(size_t x, const std::nothrow_t& nothrow_value) throw() {
  46. cout<<"new 2 operator called";
  47. return new DumbClass();
  48. //return DataPool::malloc();
  49. }
  50.  
  51. void Data::operator delete(void* ptr) {
  52. //DataPool::free(ptr);
  53. free(ptr);
  54. }
  55.  
  56. int main() {
  57. Data* data = new Data(1, 2);
  58. return 0;
  59. //Data* data = new Data(1);
  60. delete data;
  61. data = new (std::nothrow) Data(1, 2);
  62. delete data;
  63. }
  64.  
  65.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
new 1 operator calledConstructor 2 1;2