fork download
  1.  
  2. //test.cpp
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. typedef vector<int> Valarray;
  10.  
  11. template<typename L, typename R>
  12. struct BinOpPlus {
  13. const L& left;
  14. const R& right;
  15.  
  16. BinOpPlus(const L& l, const R& r)
  17. : left(l), right(r)
  18. {}
  19.  
  20. int operator[](int i) const {
  21. int l = left[i];
  22. //cerr << "Left: " << l << endl; //uncomment to fix segfault
  23. int r = right[i];
  24. //cerr << "Right: " << r << endl; //uncomment to fix segfault
  25. return l + r;
  26. }
  27. };
  28.  
  29. template<typename L, typename R>
  30. BinOpPlus<L, R> operator+(const L& left, const R& right){
  31. return BinOpPlus<L, R>(left, right);
  32. }
  33.  
  34. int main() {
  35. //int size = 10000000;
  36. int size = 10;
  37. Valarray v[3];
  38. for(int n=0; n<3; ++n){
  39. for(int i=0; i<size; ++i){
  40. int val = rand() % 100;
  41. v[n].push_back(val);
  42. }
  43. }
  44.  
  45. auto out = v[0] + v[1] + v[2];
  46.  
  47. int sum = 0;
  48. for(int i=0; i<size; ++i){
  49. cerr << "Checkpoint!" << endl;
  50. sum += out[i]; //segfaults here
  51. cerr << "Sum: " << sum << endl;
  52. }
  53.  
  54. cout << "Sum: " << sum << endl;
  55. return 0;
  56. }
Runtime error #stdin #stdout #stderr 0s 3272KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Checkpoint!