title: In C++, should I bother to cache variables, or let the compiler do the optimization? (Aliasing)
body: <p>Consider the following code (<code>p</code> is of type <code>unsigned char*</code> and <code>bitmap-&gt;width</code> is of some integer type, exactly which is unknown and depends on which version of some external library we're using):</p>
  
  <pre><code>for (unsigned x = 0;  x &lt; static_cast&lt;unsigned&gt;(bitmap-&gt;width);  ++x)
  {
      *p++ = 0xAA;
      *p++ = 0xBB;
      *p++ = 0xCC;
  }
  </code></pre>
  
  <p><sub><s>Is it worth optimizing it [..]</s></sub></p>
  
  <p>Could there be a case where this could yield more efficient results by writing:</p>
  
  <pre><code>unsigned width(static_cast&lt;unsigned&gt;(bitmap-&gt;width));
  for (unsigned x = 0;  x &lt; width;  ++x)
  {
      *p++ = 0xAA;
      *p++ = 0xBB;
      *p++ = 0xCC;
  }
  </code></pre>
  
  <p>... or is this trivial for the compiler to optimize?</p>
  
  <p><sub><s>What would you consider to be "better" code?</s></sub></p>
  
  <p><sub><em>Note from editor (Ike): for those wondering about the strikeout text, the original question, as phrased, was dangerously close to off-topic territory and was very close to being closed in spite of positive feedback. These have been stricken out. Yet please do not punish the answerers who addressed these stricken sections of the question.</em></sub></p>