fork download
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <exception>
  4. #include <new>
  5. using namespace std;
  6.  
  7. #define TRACE() cout << __LINE__ << "\t" << __FUNCTION__ << endl
  8. #define TRACE1(arg) cout << __LINE__ << "\t" << arg << endl
  9.  
  10. class AlBase {
  11. protected:
  12. AlBase() {}
  13. ~AlBase() {}
  14. public:
  15. static void* operator new(size_t size) {
  16. TRACE();
  17. if(size == 0) size = 1;
  18. if(auto ptr = malloc(size)) return ptr;
  19. throw bad_alloc();
  20. }
  21. static void operator delete(void* ptr) {
  22. TRACE();
  23. free(ptr);
  24. }
  25. };
  26.  
  27. class PAlBase {
  28. protected:
  29. PAlBase() {}
  30. ~PAlBase() {}
  31. public:
  32. static void* operator new(size_t, void* dst) {
  33. TRACE();
  34. return dst;
  35. }
  36. static void operator delete(void*, void*) {
  37. TRACE();
  38. }
  39. };
  40.  
  41. class C1 {
  42. public:
  43. C1() { TRACE(); }
  44. virtual ~C1() { TRACE(); }
  45. };
  46.  
  47. class C2 : public C1, public AlBase {
  48. public:
  49. C2() { TRACE(); }
  50. virtual ~C2() { TRACE(); }
  51. };
  52.  
  53. class C3 {
  54. public:
  55. C3() { TRACE(); }
  56. virtual ~C3() { TRACE(); }
  57. };
  58.  
  59. class C4 : public C3 {
  60. public:
  61. C4() { TRACE(); }
  62. ~C4() { TRACE(); }
  63. };
  64.  
  65. class C5 : public PAlBase {
  66. public:
  67. C5(bool raise = false) {
  68. TRACE();
  69. if(raise) throw 0;
  70. }
  71. ~C5() { TRACE(); }
  72. };
  73.  
  74. int main() {
  75. TRACE1("--------");
  76. {
  77.  
  78. // new -> C1.ctor -> C1.vtor -> delete
  79.  
  80. delete new C1;
  81.  
  82. }
  83.  
  84. TRACE1("--------");
  85. {
  86.  
  87. // AlBase.new -> C1.ctor -> C2.ctor ->
  88. // C2.vtor -> C1.vtor -> AlBase.delete
  89.  
  90. delete new C2;
  91.  
  92. }
  93.  
  94. TRACE1("--------");
  95. {
  96.  
  97. // placement new -> C3.ctor -> C3.vtor
  98.  
  99. char t1[sizeof(C3)];
  100. (new (t1) C3)->~C3();
  101.  
  102. }
  103.  
  104. TRACE1("--------");
  105. {
  106.  
  107. // placement new -> C3.ctor -> C4.ctor -> C4.vtor -> C3.vtor
  108.  
  109. char t2[sizeof(C4)];
  110. (new (t2) C4)->~C4();
  111.  
  112. }
  113.  
  114. TRACE1("--------");
  115. {
  116.  
  117. // PAlBase.new -> C5.ctor -> C5.dtor
  118.  
  119. char t3[sizeof(C5)];
  120. (new (t3) C5)->~C5();
  121.  
  122. }
  123.  
  124. TRACE1("--------");
  125. {
  126.  
  127. // PAlBase.new -> C5.ctor -> throw ->
  128. // C5.dtor + PAlBase.delete
  129.  
  130. char t4[sizeof(C5)];
  131. try {
  132. auto p5 = new (t4) C5(true);
  133. p5->~C5(); // never arrive
  134. } catch(int) {
  135. TRACE1("* caught exception");
  136. }
  137.  
  138. }
  139. }
  140.  
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
75	--------
43	C1
44	~C1
84	--------
16	operator new
43	C1
49	C2
50	~C2
44	~C1
22	operator delete
94	--------
55	C3
56	~C3
104	--------
55	C3
61	C4
62	~C4
56	~C3
114	--------
33	operator new
68	C5
71	~C5
124	--------
33	operator new
68	C5
37	operator delete
135	* caught exception