title: Why is the loop not being vectorized?
body: <p>I know that vectorization can only take place if the objects being accessed are contiguous in memory. I have created a struct which has pointer and then I create a vector of this struct and to ensure that the pointers inside the vector of objects are pointing to contiguous data blocks I set them to point to elements in a vector of double with the same size. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct Vec {
     Vec() {}
     double* a;
  };
  
  int main(int argc, char* argv[]) {
     std::vector&lt;double&gt; vec_double(10000000, 1.0);
     std::vector&lt;Vec&gt; vec_vec(10000000);
     for (unsigned i = 0; i &lt; 10000000; ++i)
        vec_vec[i].a = &amp;(vec_double[i]);
  
     // Why is this loop not vectorized
     for (unsigned i = 0; i &lt; 10000000; ++i)
        vec_double[i] += *(vec_vec[i].a);
  
     double sum = 0.0;
     for (unsigned i = 0; i &lt; 10000000; ++i)
        sum += vec_double[i];
     std::cout &lt;&lt; sum &lt;&lt; std::endl;
  
     return 0;
  }
  </code></pre>
  
  <p>However, even with O3 optimization the loop at line number 16 is not getting vectorized. Can someone please explain why is this happening?</p>