Concept

Volatile (computer programming)

Summary
In computer programming, volatile means that a value is prone to change over time, outside the control of some code. Volatility has implications within function calling conventions, and also impacts how variables are stored, accessed and cached. In the C, C++, C#, and Java programming languages, the volatile keyword indicates that a value may change between different accesses, even if it does not appear to be modified. This keyword prevents an optimizing compiler from optimizing away subsequent reads or writes and thus incorrectly reusing a stale value or omitting writes. Volatile values primarily arise in hardware access (memory-mapped I/O), where reading from or writing to memory is used to communicate with peripheral devices, and in threading, where a different thread may have modified a value. Despite being a common keyword, the behavior of volatile differs significantly between programming languages, and is easily misunderstood. In C and C++, it is a type qualifier, like const, and is a property of the type. Furthermore, in C and C++ it does not work in most threading scenarios, and that use is discouraged. In Java and C#, it is a property of a variable and indicates that the object to which the variable is bound may mutate, and is specifically intended for threading. In the D programming language, there is a separate keyword shared for the threading usage, but no volatile keyword exists. In C, and consequently C++, the volatile keyword was intended to: allow access to memory-mapped I/O devices allow uses of variables between setjmp and longjmp allow uses of sig_atomic_t variables in signal handlers. Since variables marked as volatile are prone to change outside the standard flow of code, the compiler has to perform every read and write to the variable as indicated by the code. Any access to volatile variables cannot be optimised away, e.g. by use of registers for storage of intermediate values. While intended by both C and C++, the C standards fail to express that the volatile semantics refer to the lvalue, not the referenced object.
About this result
This page is automatically generated and may contain information that is not correct, complete, up-to-date, or relevant to your search query. The same applies to every other page on this website. Please make sure to verify the information with EPFL's official sources.