Use of the volatile qualifier
ID |
c.correctness.avoid_volatile |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Type Qualifier |
Language |
C / C++ |
Rationale
volatile only tells the compiler not to cache a value in a register or reorder accesses to it around observable side effects; it says nothing about atomicity, memory ordering between threads, or visibility guarantees across cores. Code that reaches for volatile to make a variable safe to share between threads compiles but is not thread-safe, and the resulting data race is undefined behavior. Safety-critical and embedded coding standards restrict volatile to its one legitimate use — a genuine memory-mapped register or a signal handler variable — and forbid it everywhere else.
The following code illustrates the pattern detected by this rule:
void f1(void) {
// FLAGGED: Use of the volatile qualifier
volatile int x;
x = 1;
}