Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile and compiler optimization

Is it OK to say that 'volatile' keyword makes no difference if the compiler optimization is turned off i.e (gcc -o0 ....)?

I had made some sample 'C' program and seeing the difference between volatile and non-volatile in the generated assembly code only when the compiler optimization is turned on i.e ((gcc -o1 ....).

like image 885
Vivek Gupta Avatar asked Dec 11 '22 19:12

Vivek Gupta


1 Answers

No, there is no basis for making such a statement.

volatile has specific semantics that are spelled out in the standard. You are asserting that gcc -O0 always generates code such that every variable -- volatile or not -- conforms to those semantics. This is not guaranteed; even if it happens to be the case for a particular program and a particular version of gcc, it could well change when, for example, you upgrade your compiler.

like image 106
NPE Avatar answered Dec 22 '22 21:12

NPE