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++

Description

Reports any variable or field declared with the volatile qualifier.

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;
}

Remediation

For inter-thread communication, use std::atomic<T> (C++) or an explicit mutex/lock instead of volatile. Reserve volatile for its one legitimate use: a memory-mapped hardware register, or a sig_atomic_t variable touched by a signal handler.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved