fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main() {
  9.  
  10. vector<string> arr;
  11.  
  12. arr.push_back("one");
  13. arr.push_back("two");
  14. arr.push_back("three");
  15.  
  16. auto& x = arr[2];
  17.  
  18. cout << "addr x: " << &x << endl;
  19.  
  20. /*
  21. for (int i=0; i<1000; ++i) {
  22.   arr.push_back("num: " + i);
  23.   }*/
  24.  
  25. cout << "addr x: " << &x << endl;
  26.  
  27. auto& x2 = arr[2];
  28.  
  29.  
  30. cout << "addr x2: " << &x2 << endl;
  31.  
  32. cout << x << endl; // segfault arr had been reallocated x points to freed memory
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
addr x: 0x9f22a80
addr x: 0x9f22a80
addr x2: 0x9f22a80
three