title: Why isn't this zeroization code optimized out by most c compilers?
body: <p>Many crypto libraries include code similar to the following snippet:</p>
  
  <pre class="lang-c prettyprint-override"><code>/* Implementation that should never be optimized out by the compiler */
  static void optimize_proof_zeroize( void *v, size_t n )
  {
      volatile unsigned char *p = v;
      while( n-- ) *p++ = 0;
  }
  </code></pre>
  
  <p>But my naive implementation doesn't survive an optimizing compiler:</p>
  
  <pre class="lang-c prettyprint-override"><code>/* Naive zeroization implementation */
  static void naive_zeroize( unsigned char *c, size_t n)
  {
      int i;
      for( i = 0; i &lt; n; i++ )
         c[i] = 0;
  }
  </code></pre>
  
  <p>The code is used to zeroize sensitive data before freeing the memory. Since the buffer is not used again, optimizing compilers assume that they can safely remove the zeriozation from the compiled code. </p>
  
  <p>What prevents the first implemention from being optimized out?</p>