title: Segmentation Fault while pushing elements into vector
body: <p>**Segmentation fault for vector <em>amicable</em> **</p>
  <p>While running this code I'm getting runtime error <strong>segmentation fault</strong> and I think based on the error I'm getting I think its from <em>amicable</em> vector. can anyone help me to understand why I'm getting segmentation fault and helping to find solution is much appreciated.</p>
  <pre><code>#include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  int factors(int x) {
      vector &lt;int&gt; result;
      int i = 1;
      while(i*i &lt;= x) {
          if(x % i == 0) {
              result.push_back(i);
              if(x/i != i) {
                  result.push_back(x/i);
              }
          }
          i++;
      }
      int sum=0;
      for(int p=0;p&lt;result.size();p++)
          sum = sum + result[p];
      return sum - x;
  }
  
  int main() {
      
      int tests;
      cin &gt;&gt; tests;
      vector&lt;int&gt; sumar;
      for(int i=0;i&lt;=100000;i++){
          if(i==0){
              sumar.push_back(0);
          }
          else {
              sumar.push_back(factors(i));
          } 
      }
      vector&lt;int&gt; amicable;
      for(int i=1;i&lt;=100000;i++){
          int j = sumar[i];
          int c = sumar[j];
          if(i==c &amp;&amp; i!=j){
              //cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; j &lt;&lt; endl;
              amicable.push_back(i);
          }
      }
      
      while (tests--){
          int n;
          cin &gt;&gt; n;
          int sum = 0;
          for(auto i : amicable){
              if(i&gt;n)
                  break;
              sum += i;
          }
          cout &lt;&lt; sum &lt;&lt; endl;
      }
      
      return 0;
  }
  </code></pre>
  <p>I tried decreasing the limit from 100000 to 45000, its working fine with limit 45000
  but I need values till 100000</p>
  <p>thank you.</p>