Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a page that describes aliasing very thoroughly <a href="http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html" rel="nofollow noreferrer">here</a>.</p> <p>There are also some SO topics <a href="https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule">here</a> and <a href="https://stackoverflow.com/questions/262379/when-is-char-safe-for-strict-pointer-aliasing">here</a>.</p> <p>To summarize, the compiler cannot assume the value of data when two pointers of different types are accessing the same location (i.e. it must read the value every time and therefore cannot make optimizations).</p> <p>This only occurs when strict aliasing is not being enforced. Strict aliasing options:</p> <ul> <li>gcc: <strong>-fstrict-aliasing [default]</strong> and <strong>-fno-strict-aliasing</strong></li> <li>msvc: <strong>Strict aliasing is off by default.</strong> (If somebody knows how to turn it on, please say so.)</li> </ul> <hr> <h1>Example</h1> <p>Copy-paste this code into main.c:</p> <pre><code>void f(unsigned u) { unsigned short* const bad = (unsigned short*)&amp;u; } int main(void) { f(5); return 0; } </code></pre> <p>Then compile the code with these options:</p> <blockquote> <p>gcc main.c -Wall -O2</p> </blockquote> <p>And you will get:</p> <blockquote> <p>main.c:3: warning: dereferencing type-punned pointer will break strict-aliasing rules</p> </blockquote> <p>Disable aliasing with:</p> <blockquote> <p>gcc main.c -fno-strict-aliasing -Wall -O2</p> </blockquote> <p>And the warning goes away. (Or just take out -Wall but...don't compile without it)</p> <p>Try as I might I could not get MSVC to give me a warning.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload