title: Can a C++ Compiler Eliminate a Volatile Local Var that is not Read
body: <p>Say, I have this code:</p>
  
  <pre><code>int f() {
    volatile int c;
    c=34;
    return abc();
  }
  </code></pre>
  
  <p>The <strong>volatile int c</strong> is never read.  But it is marked as <strong>volatile</strong>, can the compiler eliminate it altogether?  My testing in Visual C++ 2010 shows contradictory results.  In VC++, if I enable optimization (maximizing speed) the above function contains a local variable called c (by looking at the generated assembly listing).  But, instead of using assignment operator, I also tried to initialize the variable by a compiler intrinsic function like memset() (and enable using compiler intrinsic functions), the variable is eliminated.</p>
  
  <pre><code>int f() {
    volatile int c;
    memset((void*)&amp;c,34, 1); 
    return abc();
  }
  </code></pre>
  
  <p>So according to the C++ standard, can compiler eliminate the volatile int c? I'm thinking probably there is some inconsistent behaviour in VC++ related to how intrinsic functions optimize volatile variable.</p>