As described in-depth in rule DCL34-C. Use volatile for data that cannot be cached, a volatile
-qualified variable "shall be evaluated strictly according to the rules of the abstract machine" [ISO/IEC 9899:2011]. In other words, the volatile
qualifier is used to instruct the compiler to not make caching optimizations about a variable.
However, as demonstrated in "Volatiles are miscompiled, and what to do about it" [Eide and Regehr], all tested compilers generated some percentage of incorrect compiled code with regard to volatile
accesses. Therefore, it is necessary to know how your compiler behaves when the standard volatile
behavior is required. There is also The authors also provide a workaround that eliminates some or all of these errors [Eide and Regehr].
Noncompliant Code Example
As demonstrated in [ Eide and Regehr]'s work, the following code example compiles incorrectly using GCC version 4.3.0 for IA32 and the -Os
optimization flag:
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL17-C | medium | probable | high | P4 | L3 |
Related Guidelines
ISO/IEC 9899:2011 Section Section 6.7.3, "Type qualifiers"
Bibliography
...