fork download
  1. /*
  2.  * List.h
  3.  *
  4.  * Created on: Dec 12, 2012
  5.  * Author: MrD
  6.  */
  7.  
  8. #ifndef LIST_H_
  9. #define LIST_H_
  10.  
  11. class List {
  12. long j;
  13. public:
  14. List * next;
  15. List * previous;
  16. virtual long jj() {
  17. return this->j;
  18. }
  19. List() {
  20. next = previous = 0;
  21. j = 0; // it still initializes it to 0 - mostly :)
  22. }
  23. virtual ~List() {
  24. if (next) {
  25. next->previous = this->previous;
  26. }
  27. if (previous) {
  28. previous->next = this->next;
  29. }
  30. }
  31. };
  32.  
  33. #endif /* LIST_H_ */
  34.  
  35. #include <time.h>
  36. #include<iostream>
  37. #include <exception>
  38.  
  39. using namespace std;
  40.  
  41. const static int EVERY = 10000;
  42.  
  43. void print(List * begin) {
  44. List * biter = begin;
  45. while (biter != 0) {
  46. cout << biter << " : " << biter->previous << "," << biter->next << endl;
  47. biter = biter->next;
  48. }
  49. }
  50. void term_func() {
  51. cout << "NO USE YOU WON'T SEE ME" << endl;
  52. }
  53. //void handler() {
  54. // std::cout << "Memory allocation failed, terminating\n";
  55. //// std::set_new_handler(nullptr);
  56. //}
  57. void deleteNodes(bool slow, List* iter) {
  58. iter = iter->next; // do not delete the beginning
  59. for (unsigned int var2 = 0; iter; ++var2) {
  60. if ((bool) (var2 % EVERY) == slow) {
  61. List* temp = iter->next;
  62. delete iter;
  63. iter = temp;
  64. } else
  65. iter = iter->next;
  66. }
  67. }
  68.  
  69. void traverse(List* iter, List* const BEGIN) {
  70. iter = BEGIN;
  71. long val = 0;
  72. for (List *iter2 = BEGIN; iter2; iter2 = iter2->next) {
  73. if (++val > 87910000) {
  74. // print(iter);
  75. cout << val << endl;
  76. cout << iter2 << endl;
  77. // return;
  78. }
  79. // if ((*iter).jj() && (*iter).jj() != val) {
  80. // val = (*iter).jj();
  81. // cout << (*iter).jj() << endl;
  82. // }
  83. }
  84. }
  85.  
  86. int main(int argc, char **argv) {
  87. cout << "size of List c++ : " << sizeof(List) << endl;
  88. set_terminate(term_func);
  89. // std::set_new_handler(handler);
  90. clock_t tStart = clock();
  91. List * ptrList = new List();
  92. List * const BEGIN = ptrList;
  93. unsigned long var = 0;
  94. // try {
  95. // while (true) {
  96. // new int[100000000ul];
  97. // }
  98. // } catch (const std::bad_alloc& e) {
  99. // std::cout << "Allocation failed: " << e.what() << '\n';
  100. // }
  101. try {
  102. for (;; ++var) {
  103. // List * ptrList2 = new List();
  104. List * ptrList2 = new (nothrow) List();
  105. if (!ptrList2) {
  106. cout << "out of memory - created " << var << " nodes" << endl;
  107. break;
  108. }
  109. ptrList->next = ptrList2;
  110. ptrList2->previous = ptrList;
  111. ptrList = ptrList2;
  112. // if (!(var % 1000))
  113. // cout << var << endl;
  114. }
  115. } catch (bad_alloc const& e) {
  116. // there is an exception I can't catch for the life of me
  117. // I get it around 87921000 - differing on Eclipse and cmd
  118. // Win7 prof x64
  119. cout << "caught : " << e.what() << endl;
  120. // } catch (...) { //this won't work either
  121. }
  122. //print(BEGIN);
  123. if (argc < 2) {
  124. List * iter = BEGIN;
  125. cout << "Fast delete" << endl;
  126. deleteNodes(false, iter);
  127. //print(BEGIN);
  128. cout << "Fast traverse" << endl;
  129. traverse(iter, BEGIN);
  130. } else {
  131. cout << "slow" << endl;
  132. List * iter = BEGIN;
  133. cout << "Slow delete" << endl;
  134. deleteNodes(true, iter);
  135. print(BEGIN);
  136. cout << "Slow traverse" << endl;
  137. traverse(iter, BEGIN);
  138. }
  139. cout << "Time taken: " << (double) (clock() - tStart) / CLOCKS_PER_SEC
  140. << endl;
  141.  
  142. return 0;
  143. }
  144.  
Success #stdin #stdout 1.37s 262080KB
stdin
Standard input is empty
stdout
size of List c++ : 16
out of memory - created 11066878 nodes
Fast delete
Fast traverse
Time taken: 1.36