title: SSE2 - 16-byte aligned dynamic allocation of memory
body: <p><strong>EDIT:</strong> </p>
  
  <p>This is a followup to <a href="https://stackoverflow.com/questions/1874882/sse2-compiler-error">SSE2 Compiler Error</a></p>
  
  <p>This is the <strong>real</strong> bug I experienced before and have reproduced below by changing the <code>_mm_malloc</code> statement as <a href="https://stackoverflow.com/questions/1876433/sse2-16-byte-aligned-dynamic-allocation-of-memory/1876448#1876448">Michael Burr</a> suggested:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x00415116 in SO.exe: 0xC0000005: Access violation reading 
    location 0xffffffff.</p>
  </blockquote>
  
  <p>At line <code>label: movdqa xmm0, xmmword ptr [t1+eax]</code></p>
  
  <p>I'm trying to dynamically allocate <code>t1</code> and <code>t2</code> and <a href="http://www.developers.net/intelisnshowcase/view/116" rel="nofollow noreferrer">according to this tutorial</a>, I've used <code>_mm_malloc</code>:</p>
  
  <pre><code>#include &lt;emmintrin.h&gt;
  int main(int argc, char* argv[])
  { 
   int *t1, *t2;
   const int n = 100000;
   t1 = (int*)_mm_malloc(n*sizeof(int),16);
   t2 = (int*)_mm_malloc(n*sizeof(int),16);
   __m128i mul1, mul2;
    for (int j = 0; j &lt; n; j++)
    {
    t1[j] = j;
    t2[j] = (j+1);
    } // set temporary variables to random values
    _asm
    {
     mov eax, 0
     label: movdqa xmm0, xmmword ptr [t1+eax]
     movdqa xmm1, xmmword ptr [t2+eax]
     pmuludq xmm0, xmm1
     movdqa mul1, xmm0
     movdqa xmm0, xmmword ptr [t1+eax]
     pshufd xmm0, xmm0, 05fh
     pshufd xmm1, xmm1, 05fh
     pmuludq xmm0, xmm1
     movdqa mul2, xmm0
     add eax, 16
     cmp eax, 100000
     jnge label
    }
       _mm_free(t1);
       _mm_free(t2);
  
   return 0;
  }
  </code></pre>