Volatile Expressions

Marking something as volatile prevents the compiler from optimizing accesses related to that variable/expression.

Use cases

You might want to opt out from code optimization when:

  • you are doing some low-level programming (the "redundant" reads/writes to certain addresses may cause side-effects in the system)
  • the variable in question may be changed from external sources that the compiler is not aware of

Examples

Example 1

The following code:

int foo(int& x) {
  x = 10;
  x = 20;
  auto y = x;
  y = x;
  return y;
}

might be simplified to:

int foo(int& x) {
  x = 20;
  return x;
}

To prevent that from happening, you can do:

// reads/writes to `x` may cause
// side-effects in the system
int foo(volatile int& x) {
  x = 10;
  x = 20;
  auto y = x;
  y = x;
  return y;
}

Example 2

The following code:

int some_int = 100;
while (some_int == 100) {
  doSomething();
}

may be optimized to:

while (true) {
  doSomething();
}

To prevent that from happening, you can do:

// `some_int` may be changed from external source
volatile int some_int = 100;
while (some_int == 100) {
  doSomething();
}

References