How to prevent compiler from optimizing a load to variable that is never used <p>Intro: Im trying to quick hack fix old code and use __try MSVC extension to check if some ptr points to some legit memory or if *ptr will cause memory violation(if so I drop processing of this ptr). So I wrote something like:</p> <pre><code>bool checkIsPtrPointingToValidAddress(const void *ptr) { __try { auto cpy = *((int*)ptr); // force mem access... if ( (cpy ==42) &amp;&amp; ((rand()+rand()+rand()+rand()+ rand()) == 1)) { FILE* pFile = fopen ("tempdata.dat","w"); //... by unlikely but possible action fputs (" ",pFile); fclose (pFile); } return true; } __except(1) { return false; } } </code></pre> <p>Thing is that my solution to force mem access seems weird, ugly, and as a bonus I'm not sure it is correct. Also please not I can't disable optimizations on the entire project, so that is not an option. And documentation for pragma optimize on MSDN sucks, aka it is not clear if "" disables all optimizations on the function. </p>