fork download
  1. // Lab 01, testing with valgrind
  2.  
  3. // This program uses a Mersenne Twister to generate pseudo-random numbers.
  4. // The fixed seed means it will generate the same sequence every time, on
  5. // any computer system which properly implements the mt19937 algorithm.
  6.  
  7. // The random numbers are used as indices into a block of memory (of size
  8. // 100), but the random numbers will be in the range [0, 110)! If it can
  9. // successfully initialize 10 elements of the array, it will then generate
  10. // more random numbers and display the values at those indices. If any of
  11. // this second batch of random indices didn't occur in the first 10, the
  12. // program would be trying to output an uninitialized value.
  13.  
  14. // Eventually something will go wrong. Your task is to use valgrind to
  15. // determine which problem occurs, and which line of code causes the first
  16. // problem to be reported.
  17.  
  18. // Make sure you don't modify this file! Adding or removing lines of
  19. // code could cause you to report the wrong line number for your answers.
  20. // The first initialization of an element of data should occur on line 60,
  21. // and the first output of a value from the array should occur on line 72.
  22.  
  23. // To run this test, type the following commands:
  24. // make valgrind
  25. // valgrind ./lab1_bad_valgrind |& more
  26.  
  27. // You've probably never seen "|& more" before. This means "when you run
  28. // the specified program, send (pipe, vertical bar) the normal and error
  29. // output (the ampersand) to the program more". More is a "paginator"
  30. // that will stop after every page. This means you don't have to scroll
  31. // back to find the first error, it will display one page and stop, waiting
  32. // for you to decide when to display "more" of the output. Press the space
  33. // bar to go onto the next page, or "q" (with no quote marks) to quit.
  34.  
  35. // You can also use this if building your program is producing a lot of
  36. // errors and you're having trouble scrolling back. You can type:
  37. // make |& more
  38. // If there's no errors, you probably won't have to hit the spacebar or q,
  39. // since there should be less than one page of output. You can also use
  40. // more with a filename, such as the command:
  41. // more lab1_bad.cpp
  42.  
  43.  
  44. #include <iostream>
  45. #include <vector>
  46. #include <random>
  47. using namespace std;
  48.  
  49.  
  50. constexpr size_t SIZE = 100;
  51. constexpr size_t MOD = 110;
  52.  
  53.  
  54. int main() {
  55. mt19937 mt(72521278);
  56. int *data = new int[SIZE];
  57. int count = 0;
  58.  
  59. // Initialize some of the elements, possibly segfaulting
  60. data[mt() % MOD] = count++;
  61. data[mt() % MOD] = count++;
  62. data[mt() % MOD] = count++;
  63. data[mt() % MOD] = count++;
  64. data[mt() % MOD] = count++;
  65. data[mt() % MOD] = count++;
  66. data[mt() % MOD] = count++;
  67. data[mt() % MOD] = count++;
  68. data[mt() % MOD] = count++;
  69. data[mt() % MOD] = count++;
  70.  
  71. // Output some of the elements, possibly getting an uninitialized value
  72. cout << data[mt() % MOD] << endl;
  73. cout << data[mt() % MOD] << endl;
  74. cout << data[mt() % MOD] << endl;
  75. cout << data[mt() % MOD] << endl;
  76. cout << data[mt() % MOD] << endl;
  77. cout << data[mt() % MOD] << endl;
  78. cout << data[mt() % MOD] << endl;
  79. cout << data[mt() % MOD] << endl;
  80. cout << data[mt() % MOD] << endl;
  81. cout << data[mt() % MOD] << endl;
  82.  
  83. delete [] data;
  84. return 0;
  85. } // main()
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1
2
2
8
6
7
2
0
0
0