fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. char buf[10000];
  6. char *ptr = buf;
  7. int freepos[1000];
  8. int freecount = 0;
  9.  
  10. void* myMalloc( size_t size ) {
  11. int *sz = NULL;
  12. void *tmp = NULL;
  13. char *ctmp;
  14. int i;
  15. for (i = freecount - 1; i >= 0; i--) {
  16. sz = (int*)freepos[i];
  17. ctmp = (char*)sz;
  18. ctmp += sizeof(int);
  19. tmp = ctmp;
  20. cout << "check free - index:" << i
  21. << " pos:" << (int)tmp - (int)buf
  22. << " size:" << *sz << endl;
  23. if (*sz == size) {
  24. freepos[i] = freepos[--freecount];
  25. break;
  26. }
  27. }
  28. if (i < 0) {
  29. sz = (int*)ptr;
  30. *sz = size;
  31. ptr += sizeof(int) + size;
  32. }
  33. ctmp = (char*)sz;
  34. ctmp += sizeof(int);
  35. tmp = ctmp;
  36. cout << "call myAlloc - pos:" << (int)tmp - (int)buf
  37. << " size:" << *sz
  38. << " / recycle:" << i << endl;
  39. return tmp;
  40. }
  41.  
  42. void myFree( void* p) {
  43. char *ctmp = (char*)p;
  44. ctmp -= sizeof(int);
  45. int *itmp = (int*)ctmp;
  46. freepos[freecount++] = (int)itmp;
  47. cout << "call myFree - pos:" << (int)p - (int)buf
  48. << " size:" << *itmp
  49. << " / freecount:" << freecount << endl;
  50. }
  51.  
  52. class Foo
  53. {
  54. static int c() {
  55. static int cc = 1;
  56. return cc++;
  57. }
  58. int _id;
  59. public:
  60. Foo() : _id( c() ) { cout << "new: " << _id << endl; }
  61. ~Foo() { cout << "del: " << _id << endl; }
  62. int id() const { return _id; }
  63. static void* operator new( size_t size ) {
  64. cout << "call new" << endl;
  65. return myMalloc( size );
  66. }
  67. static void operator delete( void* p ) {
  68. cout << "call delete - id:" << ((Foo*)p)->_id << endl;
  69. myFree( p );
  70. }
  71. };
  72.  
  73. class Bar : public Foo
  74. {
  75. int n;
  76. public:
  77. Bar() : n( 0 ) {}
  78. ~Bar() { cout << "BAR!!" << endl; }
  79. };
  80.  
  81. class Baz : public Bar
  82. {
  83. int k;
  84. public:
  85. Baz() : k( 1 ) {}
  86. };
  87.  
  88. int main() {
  89.  
  90. Foo foo1, foo2;
  91. Foo *foo3 = ( Foo* )myMalloc( sizeof( Foo ) ); // コンストラクタが呼ばれない
  92. Foo *foo4 = ( Foo* )malloc( sizeof( Foo ) ); // コンストラクタが呼ばれない
  93. Foo *foo5 = ::new Foo;
  94. Foo *foo6 = new Foo; // Foo::new
  95. Baz *baz1 = new Baz; // なんと Foo::new が呼ばれる
  96. Baz baz2;
  97.  
  98. Baz *baz3 = new Baz;
  99. Foo *foo7 = new Foo;
  100. Bar *bar1 = new Bar;
  101. Foo *foo8 = new Foo;
  102. delete bar1;
  103. delete foo7;
  104. delete baz3;
  105. baz3 = new Baz;
  106. foo7 = new Foo;
  107.  
  108.  
  109. cout << foo1.id() << endl;
  110. cout << foo2.id() << endl;
  111. cout << foo3->id() << endl;
  112. cout << foo4->id() << endl;
  113. cout << foo5->id() << endl;
  114. cout << foo6->id() << endl;
  115.  
  116. myFree( foo3 );
  117. free( foo4 );
  118. ::delete foo5;
  119. delete foo6; // Foo::delete
  120.  
  121. cout << "[baz]" << endl;
  122.  
  123. delete baz1; // なんと Foo::delete が呼ばれる
  124.  
  125. cout << "[other]" << endl;
  126.  
  127. delete bar1;
  128. delete baz3;
  129. delete foo7;
  130. delete foo8;
  131.  
  132. cout << "[done]" << endl;
  133.  
  134. return 0;
  135. }
Success #stdin #stdout 0s 3488KB
stdin
Standard input is empty
stdout
new: 1
new: 2
call myAlloc - pos:4 size:4 / recycle:-1
new: 3
call new
call myAlloc - pos:12 size:4 / recycle:-1
new: 4
call new
call myAlloc - pos:20 size:12 / recycle:-1
new: 5
new: 6
call new
call myAlloc - pos:36 size:12 / recycle:-1
new: 7
call new
call myAlloc - pos:52 size:4 / recycle:-1
new: 8
call new
call myAlloc - pos:60 size:8 / recycle:-1
new: 9
call new
call myAlloc - pos:72 size:4 / recycle:-1
new: 10
BAR!!
del: 9
call delete - id:9
call myFree - pos:60 size:8 / freecount:1
del: 8
call delete - id:8
call myFree - pos:52 size:4 / freecount:2
BAR!!
del: 7
call delete - id:7
call myFree - pos:36 size:12 / freecount:3
call new
check free - index:2 pos:36 size:12
call myAlloc - pos:36 size:12 / recycle:2
new: 11
call new
check free - index:1 pos:52 size:4
call myAlloc - pos:52 size:4 / recycle:1
new: 12
1
2
0
0
3
4
call myFree - pos:4 size:4 / freecount:2
del: 3
del: 4
call delete - id:4
call myFree - pos:12 size:4 / freecount:3
[baz]
BAR!!
del: 5
call delete - id:5
call myFree - pos:20 size:12 / freecount:4
[other]
BAR!!
del: 9
call delete - id:9
call myFree - pos:60 size:8 / freecount:5
BAR!!
del: 11
call delete - id:11
call myFree - pos:36 size:12 / freecount:6
del: 12
call delete - id:12
call myFree - pos:52 size:4 / freecount:7
del: 10
call delete - id:10
call myFree - pos:72 size:4 / freecount:8
[done]
BAR!!
del: 6
del: 2
del: 1