IT lexicon › Programming › volatile

volatile

Programming På svenska → Updated: 2026-11-02

A C and C++ type qualifier telling the compiler that every read and write of the variable must actually happen.

Compilers normally assume memory only changes when the program changes it. So a loop waiting on a flag can be optimised into an infinite loop, and two consecutive writes to the same address merged into one. For a hardware register, or a flag set by an interrupt, both optimisations are fatal. volatile, introduced with ANSI C in 1989, switches them off for that one variable.

That is also all it does. It provides no atomicity and no ordering relative to other variables, and the CPU is still free to reorder accesses. Threads on multiple cores need atomics and memory barriers; the Linux kernel documentation carries a whole page titled "Why the volatile type class should not be used". Java's volatile, incidentally, is a different beast with genuine ordering guarantees — same word, different promises.

← Back to the lexicon