Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The compiler can, and probably will, eliminate (ignore) the unused <code>volatile</code> variable declaration (but the compiler cannot eliminate an unused global variable definition - it has to assume that some other translation unit (TU) will reference it).</p> <p>If the variable is local to the function and unused, it can be eliminated by the compiler, regardless of its volatility. It is not clear you can have meaningful local volatile variables, though I suppose you could call a function that passes its address to some code that then arranges for an interrupt handler to write to that variable - achieving volatility (but it is then, clearly, a used variable).</p> <p>The <code>volatile</code> qualifier controls (influences) how the compiler generates the code that accesses the variable - if the code does not access the variable, there is no need to alter the code that it generates except to avoid generating a reference to the variable. It may as well not exist.</p> <hr> <p>Further thoughts:</p> <p>If the variable is <code>static volatile</code> and unreferenced in the source code, can it be eliminated?</p> <p>The answer is (almost certainly) yes. There is no reference to the variable in the source, and the only ways it can be accessed in a portable manner require it to be referenced. Possible unportable hacks would include having multiple such static variables defined, and passing a reference to one of them to some function, and that function then expects to be able to access the other variables by address manipulation. However, such code is at best gruesome and non-portable. The author of such should probably be taken out back somewhere and quietly dissuaded from writing such code again.</p> <p>So, a global variable definition cannot be eliminated; it might be referenced from another TU. A static variable definition that is unused can be eliminated. A local variable definition that is unused can be eliminated. And this applies whether the variable in question has a <code>volatile</code> qualifier or not.</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