fork(1) download
  1. #include <vector>
  2. #include <string>
  3. #include <memory>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. #define LEN 1000000
  9.  
  10. char stringlengths[LEN];
  11.  
  12. int main(void)
  13. {
  14. srand(0);
  15. int totalalloc = 0;
  16. for(int i=0; i<LEN; ++i) {
  17. stringlengths[i] = rand()%16;
  18. totalalloc += stringlengths[i];
  19. }
  20. std::cout << "total string length to be allocated: " << totalalloc << '\n';
  21. int pre, post;
  22. #if 0
  23. {
  24. std::cout << "virtual memory before: ";
  25. std::cin >> pre;
  26. std::unique_ptr<char*[]> ctest(new char*[LEN]);
  27. for(int i=0; i<LEN; ++i)
  28. ctest[i] = new char[stringlengths[i]];
  29. std::cout << "virtual memory after: ";
  30. std::cin >> post;
  31. std::cout << "char* difference of " << (post-pre) << '\n';
  32. for(int i=0; i<LEN; ++i)
  33. delete [] ctest[i];
  34. }
  35. #endif
  36. #if 0
  37. {
  38. std::cout << "virtual memory before: ";
  39. std::cin >> pre;
  40. std::unique_ptr<std::unique_ptr<char[]>[]> ctest(new std::unique_ptr<char[]>[LEN]);
  41. for(int i=0; i<LEN; ++i)
  42. ctest[i].reset(new char[stringlengths[i]]);
  43. std::cout << "virtual memory after: ";
  44. std::cin >> post;
  45. std::cout << "unique_ptr difference of " << (post-pre) << '\n';
  46. }
  47. #endif
  48. #if 1
  49. {
  50. std::cout << "virtual memory before: ";
  51. std::cin >> pre;
  52. std::unique_ptr<std::string[]> ctest(new std::string[LEN]);
  53. for(int i=0; i<LEN; ++i)
  54. ctest[i].resize(stringlengths[i]);
  55. std::cout << "virtual memory after: ";
  56. std::cin >> post;
  57. std::cout << "std::string difference of " << (post-pre) << '\n';
  58. }
  59. #endif
  60. std::cin >> pre;
  61. return 0;
  62. }
Success #stdin #stdout 0.26s 29816KB
stdin
Standard input is empty
stdout
total string length to be allocated: 7499235
virtual memory before: virtual memory after: std::string difference of -143117476