title: Dynamically aligned multi-dimensional array is not regarded as aligned by the GCC compiler
body: <p>I am new to loop vectorization. But I couldn't find answers to my simple task by searching the internet for several days.</p>
  
  <p>I have a 2-D array which has 4 doubles in the first dimension. I want to vectorize this 4 elements. If I use static array gas[32768][4] in the code below, and compile the code with</p>
  
  <blockquote>
    <p>g++ -O2 -ftree-vectorize -ftree-vectorizer-verbose=7 stack.cpp </p>
  </blockquote>
  
  <p>it shows </p>
  
  <blockquote>
    <p>vect_model_load_cost: aligned.</p>
  </blockquote>
  
  <p>for the bottom k loop. However, if I use dynamic array as shown below, it shows</p>
  
  <blockquote>
    <p>Vectorizing an unaligned access</p>
  </blockquote>
  
  <p>But, I think my dynamic array is indeed aligned.
  I wonder how to force the compiler to know that the array is indeed aligned.</p>
  
  <p>I have also tried array of structure instead of 2-D array. Again, if it is a static array, it is OK.
  But if it is a dynamically assigned array of structure using >new, the compiler won't recognize the alignment. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;new&gt;
  #include &lt;malloc.h&gt;
  #include &lt;stdio.h&gt;
  
  int main()
  {
     clock_t t;
     double temp[4];
     int ng=32768;
     int i, j, k;
  
     double **gas;
     gas = (double **)memalign(__BIGGEST_ALIGNMENT__,32768*sizeof(double*));
     gas[0] = (double *)memalign(__BIGGEST_ALIGNMENT__,32768*4*sizeof(double));
     for (i=0; i&lt;32768; i++){
       gas[i] = (double *)((unsigned char *)gas[0] + i*4*sizeof(double));
     }
  
  /*
   replace above 7 lines with static assignment: double gas[32768][4]; then the compiler recoganize that the data are aligned
  */
  
     for (i=0; i&lt;ng; i++){
       for (k=0; k&lt;4; k++){
         gas[i][k]=i*1.0;
       }
     }
     for (j=0; j&lt;10000; j++){
       for (k=0; k&lt;4; k++){
          temp[k]=gas[j][k];
          temp[k]+=gas[j+1][k];
          temp[k]+=gas[j+2][k];
          temp[k]+=gas[j+13][k];
          temp[k]+=gas[j+14][k];
          temp[k]+=gas[j+15][k];
          temp[k]+=gas[j+16][k];
       }
     }
      std::cout&lt;&lt;temp[0]&lt;&lt;" "&lt;&lt;temp[1]&lt;&lt;" "&lt;&lt;temp[2]&lt;&lt;" "&lt;&lt;temp[3]&lt;&lt;std::endl;
  }
  </code></pre>