289

What does the volatile keyword do? In C++ what problem does it solve?

In my case, I have never knowingly needed it.

4

19 Answers 19

344

volatile is needed if you are reading from a spot in memory that, say, a completely separate process/device/whatever may write to.

I used to work with dual-port ram in a multiprocessor system in straight C. We used a hardware managed 16 bit value as a semaphore to know when the other guy was done. Essentially we did this:

void waitForSemaphore()
{
   volatile uint16_t* semPtr = WELL_KNOWN_SEM_ADDR;/*well known address to my semaphore*/
   while ((*semPtr) != IS_OK_FOR_ME_TO_PROCEED);
}

Without volatile, the optimizer sees the loop as useless (The guy never sets the value! He's nuts, get rid of that code!) and my code would proceed without having acquired the semaphore, causing problems later on.

19
  • 1
    @Zyl No, it does not. In practice, what you suggest is likely what will happen. But theoretically, one could end up with a compiler that optimizes access to values because it decided that none of those values are ever changed. And if you meant volatile to apply to the value and not the pointer, you'd be screwed. Again, unlikely, but it's better to err on doing things right, than taking advantage of behavior that happens to work today.
    – iheanyi
    Commented Jun 23, 2015 at 21:56
  • 1
    @Doug T. A better explanation is this Commented Feb 18, 2016 at 7:57
  • 7
    @curiousguy it did not decide wrongly. It made the correct deduction based on the information given. If you fail to mark something volatile, the compiler is free to assume that it is not volatile. That's what the compiler does when optimizing code. If there is more information, namely, that said data is in fact volatile, it's the responsibility to the programmer to provide that information. What you're claiming to by a buggy compiler is really just bad programming.
    – iheanyi
    Commented Jul 17, 2018 at 19:54
  • 1
    @curiousguy no, just because the volatile keyword appears once doesn't mean everything suddenly becomes volatile. I gave a scenario where the compiler does the right thing and achieves a result that is contrary to what the programmer wrongly expects. Just like the "most vexing parse" is not the sign of the compiler error, neither is the case here.
    – iheanyi
    Commented Jul 17, 2018 at 23:54
  • 1
    @DavidSchwartz - so, if I get you correctly, you are suggesting kind of an alias for volatile int, as if there was a typedef volatile int atomic_int, and then say the use of volatile is not necessary? If so, then the same argument could be used to say that if the system provides a type called whole that behaves like int then using int is not necessary???! Also, I think that in my world, this won't be an appropriate use of the word atomic, as described above. Or did I completely missed your point?
    – ysap
    Commented May 24, 2020 at 14:13
102

volatile is needed when developing embedded systems or device drivers, where you need to read or write a memory-mapped hardware device. The contents of a particular device register could change at any time, so you need the volatile keyword to ensure that such accesses aren't optimised away by the compiler.

3
  • 17
    This is not only valid for embedded systems but for all device drivers development. Commented Sep 16, 2008 at 14:11
  • The only time I ever needed it on an 8bit ISA bus where you read the same address twice - the compiler had a bug and ignored it (early Zortech c++) Commented Jan 13, 2009 at 5:13
  • Volatile is very rarely adequate for control of external devices. Its semantics is wrong for modern MMIO: you have to make too many objects volatile and it hurts optimization. But modern MMIO behaves like normal memory until a flag is set so volatile should not be needed. Many drivers don't ever use volatile.
    – curiousguy
    Commented Nov 22, 2019 at 22:56
71

Some processors have floating point registers that have more than 64 bits of precision (eg. 32-bit x86 without SSE, see Peter's comment). That way, if you run several operations on double-precision numbers, you actually get a higher-precision answer than if you were to truncate each intermediate result to 64 bits.

This is usually great, but it means that depending on how the compiler assigned registers and did optimizations you'll have different results for the exact same operations on the exact same inputs. If you need consistency then you can force each operation to go back to memory by using the volatile keyword.

It's also useful for some algorithms that make no algebraic sense but reduce floating point error, such as Kahan summation. Algebraicly it's a nop, so it will often get incorrectly optimized out unless some intermediate variables are volatile.

14
  • 6
    When you compute numerical derivatives it is useful too, to make sure x + h - x == h you define hh = x + h - x as volatile so that a proper delta can be computed. Commented Jun 30, 2010 at 11:36
  • 6
    +1, indeed in my experience there was a case when floating-point computations produced different results in Debug and Release, so unit tests written for one configuration were failing for another. We solved it by means of declaring one floating-point variable as volatile double instead of just double, so to ensure that it is truncated from FPU precision to 64-bit (RAM) precision before continuing further computations. The results were substantially different because of a further exaggeration of the floating-point error. Commented Aug 6, 2015 at 7:05
  • 1
    Your definition of "modern" is a bit off. Only 32-bit x86 code that avoids SSE/SSE2 is affected by this, and it wasn't "modern" even 10 years ago. MIPS / ARM / POWER all have 64-bit hardware registers, and so does x86 with SSE2. C++ implementations x86-64 always use SSE2, and compilers have options like g++ -mfpmath=sse to use it for 32-bit x86 as well. You can use gcc -ffloat-store to force rounding everywhere even when using x87, or you can set the x87 precision to 53-bit mantissa: randomascii.wordpress.com/2012/03/21/…. Commented Jul 15, 2018 at 13:13
  • But still good answer, for obsolete x87 code-gen, you can use volatile to force rounding in a few specific places without losing the benefits everywhere. Commented Jul 15, 2018 at 13:14
  • 1
    Or do I confuse inaccurate with inconsistent?
    – user10957435
    Commented Dec 16, 2019 at 5:40
61

From a "Volatile as a promise" article by Dan Saks:

(...) a volatile object is one whose value might change spontaneously. That is, when you declare an object to be volatile, you're telling the compiler that the object might change state even though no statements in the program appear to change it."

Here are links to three of his articles regarding the volatile keyword:

1
  • These links are now all broken.
    – rand'Chris
    Commented Jul 11 at 23:41
22

You MUST use volatile when implementing lock-free data structures. Otherwise the compiler is free to optimize access to the variable, which will change the semantics.

To put it another way, volatile tells the compiler that accesses to this variable must correspond to a physical memory read/write operation.

For example, this is how InterlockedIncrement is declared in the Win32 API:

LONG __cdecl InterlockedIncrement(
  __inout  LONG volatile *Addend
);
2
  • 1
    You absolutely do NOT need to declare a variable volatile in order to be able to use InterlockedIncrement.
    – curiousguy
    Commented Jun 28, 2018 at 2:05
  • 2
    This answer is obsolete now that C++11 provides std::atomic<LONG> so you can write lockless code more safely without problems of having pure loads / pure stores optimized away, or reordered, or whatever else. Commented Jul 15, 2018 at 13:17
11

A large application that I used to work on in the early 1990s contained C-based exception handling using setjmp and longjmp. The volatile keyword was necessary on variables whose values needed to be preserved in the block of code that served as the "catch" clause, lest those vars be stored in registers and wiped out by the longjmp.

11

In Standard C, one of the places to use volatile is with a signal handler. In fact, in Standard C, all you can safely do in a signal handler is modify a volatile sig_atomic_t variable, or exit quickly. Indeed, AFAIK, it is the only place in Standard C that the use of volatile is required to avoid undefined behaviour.

ISO/IEC 9899:2011 §7.14.1.1 The signal function

¶5 If the signal occurs other than as the result of calling the abort or raise function, the behavior is undefined if the signal handler refers to any object with static or thread storage duration that is not a lock-free atomic object other than by assigning a value to an object declared as volatile sig_atomic_t, or the signal handler calls any function in the standard library other than the abort function, the _Exit function, the quick_exit function, or the signal function with the first argument equal to the signal number corresponding to the signal that caused the invocation of the handler. Furthermore, if such a call to the signal function results in a SIG_ERR return, the value of errno is indeterminate.252)

252) If any signal is generated by an asynchronous signal handler, the behavior is undefined.

That means that in Standard C, you can write:

static volatile sig_atomic_t sig_num = 0;

static void sig_handler(int signum)
{
    signal(signum, sig_handler);
    sig_num = signum;
}

and not much else.

POSIX is a lot more lenient about what you can do in a signal handler, but there are still limitations (and one of the limitations is that the Standard I/O library — printf() et al — cannot be used safely).

9

Developing for an embedded, I have a loop that checks on a variable that can be changed in an interrupt handler. Without "volatile", the loop becomes a noop - as far as the compiler can tell, the variable never changes, so it optimizes the check away.

Same thing would apply to a variable that may be changed in a different thread in a more traditional environment, but there we often do synchronization calls, so compiler is not so free with optimization.

8

I've used it in debug builds when the compiler insists on optimizing away a variable that I want to be able to see as I step through code.

8

Besides using it as intended, volatile is used in (template) metaprogramming. It can be used to prevent accidental overloading, as the volatile attribute (like const) takes part in overload resolution.

template <typename T> 
class Foo {
  std::enable_if_t<sizeof(T)==4, void> f(T& t) 
  { std::cout << 1 << t; }
  void f(T volatile& t) 
  { std::cout << 2 << const_cast<T&>(t); }

  void bar() { T t; f(t); }
};

This is legal; both overloads are potentially callable and do almost the same. The cast in the volatile overload is legal as we know bar won't pass a non-volatile T anyway. The volatile version is strictly worse, though, so never chosen in overload resolution if the non-volatile f is available.

Note that the code never actually depends on volatile memory access.

2
  • Could you please elaborate on this with an example? It'd really help me understand better. Thanks!
    – batbrat
    Commented Feb 6, 2018 at 15:39
  • "The cast in the volatile overload" A cast is an explicit conversion. It's a SYNTAX construct. Many people makes that confusion (even standard authors).
    – curiousguy
    Commented Jun 28, 2018 at 1:50
5
  1. you must use it to implement spinlocks as well as some (all?) lock-free data structures
  2. use it with atomic operations/instructions
  3. helped me once to overcome compiler's bug (wrongly generated code during optimization)
3
  • 5
    You are better off using a library, compiler intrinsics, or inline assembly code. Volatile is unreliable.
    – Zan Lynx
    Commented May 3, 2009 at 17:16
  • 2
    1 and 2 both make use of atomic operations, but volatile does not provide atomic semantics and the platform-specific implementations of atomic will supercede the need for using volatile, so for 1 and 2, I disagree, you do NOT need volatile for these.
    – user21037
    Commented Feb 13, 2011 at 16:01
  • Who says anything about volatile providing atomic semantics? I said you need to USE volatile WITH atomic operations and if you don't think it's true look at the declarations of interlocked operations of win32 API (this guy also explained this in his answer) Commented Feb 17, 2011 at 13:05
4

Other answers already mention avoiding some optimization in order to:

  • use memory mapped registers (or "MMIO")
  • write device drivers
  • allow easier debugging of programs
  • make floating point computations more deterministic

Volatile is essential whenever you need a value to appear to come from the outside and be unpredictable and avoid compiler optimizations based on a value being known, and when a result isn't actually used but you need it to be computed, or it's used but you want to compute it several times for a benchmark, and you need the computations to start and end at precise points.

A volatile read is like an input operation (like scanf or a use of cin): the value seems to come from the outside of the program, so any computation that has a dependency on the value needs to start after it.

A volatile write is like an output operation (like printf or a use of cout): the value seems to be communicated outside of the program, so if the value depends on a computation, it needs to be finished before.

So a pair of volatile read/write can be used to tame benchmarks and make time measurement meaningful.

Without volatile, your computation could be started by the compiler before, as nothing would prevent reordering of computations with functions such as time measurement.

4

All answers are excellent. But on the top of that, I would like to share an example.

Below is a little cpp program:

#include <iostream>

int x;

int main(){
    char buf[50];
    x = 8;

    if(x == 8)
        printf("x is 8\n");
    else
        sprintf(buf, "x is not 8\n");

    x=1000;
    while(x > 5)
        x--;
    return 0;
}

Now, lets generate the assembly of the above code (and I will paste only that portions of the assembly which relevant here):

The command to generate assembly:

g++ -S -O3 -c -fverbose-asm -Wa,-adhln assembly.cpp

And the assembly:

main:
.LFB1594:
    subq    $40, %rsp    #,
    .seh_stackalloc 40
    .seh_endprologue
 # assembly.cpp:5: int main(){
    call    __main   #
 # assembly.cpp:10:         printf("x is 8\n");
    leaq    .LC0(%rip), %rcx     #,
 # assembly.cpp:7:     x = 8;
    movl    $8, x(%rip)  #, x
 # assembly.cpp:10:         printf("x is 8\n");
    call    _ZL6printfPKcz.constprop.0   #
 # assembly.cpp:18: }
    xorl    %eax, %eax   #
    movl    $5, x(%rip)  #, x
    addq    $40, %rsp    #,
    ret 
    .seh_endproc
    .p2align 4,,15
    .def    _GLOBAL__sub_I_x;   .scl    3;  .type   32; .endef
    .seh_proc   _GLOBAL__sub_I_x

You can see in the assembly that the assembly code was not generated for sprintf because the compiler assumed that x will not change outside of the program. And same is the case with the while loop. while loop was altogether removed due to the optimization because compiler saw it as a useless code and thus directly assigned 5 to x (see movl $5, x(%rip)).

The problem occurs when what if an external process/ hardware would change the value of x somewhere between x = 8; and if(x == 8). We would expect else block to work but unfortunately the compiler has trimmed out that part.

Now, in order to solve this, in the assembly.cpp, let us change int x; to volatile int x; and quickly see the assembly code generated:

main:
.LFB1594:
    subq    $104, %rsp   #,
    .seh_stackalloc 104
    .seh_endprologue
 # assembly.cpp:5: int main(){
    call    __main   #
 # assembly.cpp:7:     x = 8;
    movl    $8, x(%rip)  #, x
 # assembly.cpp:9:     if(x == 8)
    movl    x(%rip), %eax    # x, x.1_1
 # assembly.cpp:9:     if(x == 8)
    cmpl    $8, %eax     #, x.1_1
    je  .L11     #,
 # assembly.cpp:12:         sprintf(buf, "x is not 8\n");
    leaq    32(%rsp), %rcx   #, tmp93
    leaq    .LC0(%rip), %rdx     #,
    call    _ZL7sprintfPcPKcz.constprop.0    #
.L7:
 # assembly.cpp:14:     x=1000;
    movl    $1000, x(%rip)   #, x
 # assembly.cpp:15:     while(x > 5)
    movl    x(%rip), %eax    # x, x.3_15
    cmpl    $5, %eax     #, x.3_15
    jle .L8  #,
    .p2align 4,,10
.L9:
 # assembly.cpp:16:         x--;
    movl    x(%rip), %eax    # x, x.4_3
    subl    $1, %eax     #, _4
    movl    %eax, x(%rip)    # _4, x
 # assembly.cpp:15:     while(x > 5)
    movl    x(%rip), %eax    # x, x.3_2
    cmpl    $5, %eax     #, x.3_2
    jg  .L9  #,
.L8:
 # assembly.cpp:18: }
    xorl    %eax, %eax   #
    addq    $104, %rsp   #,
    ret 
.L11:
 # assembly.cpp:10:         printf("x is 8\n");
    leaq    .LC1(%rip), %rcx     #,
    call    _ZL6printfPKcz.constprop.1   #
    jmp .L7  #
    .seh_endproc
    .p2align 4,,15
    .def    _GLOBAL__sub_I_x;   .scl    3;  .type   32; .endef
    .seh_proc   _GLOBAL__sub_I_x

Here you can see that the assembly codes for sprintf, printf and while loop were generated. The advantage is that if the x variable is changed by some external program or hardware, sprintf part of the code will be executed. And similarly while loop can be used for busy waiting now.

4

I would like to quote Herb Sutter's words from his GotW #95, which can help to understand the meaning of the volatile variables:

C++ volatile variables (which have no analog in languages like C# and Java) are always beyond the scope of this and any other article about the memory model and synchronization. That’s because C++ volatile variables aren’t about threads or communication at all and don’t interact with those things. Rather, a C++ volatile variable should be viewed as portal into a different universe beyond the language — a memory location that by definition does not obey the language’s memory model because that memory location is accessed by hardware (e.g., written to by a daughter card), have more than one address, or is otherwise “strange” and beyond the language. So C++ volatile variables are universally an exception to every guideline about synchronization because are always inherently “racy” and unsynchronizable using the normal tools (mutexes, atomics, etc.) and more generally exist outside all normal of the language and compiler including that they generally cannot be optimized by the compiler (because the compiler isn’t allowed to know their semantics; a volatile int vi; may not behave anything like a normal int, and you can’t even assume that code like vi = 5; int read_back = vi; is guaranteed to result in read_back == 5, or that code like int i = vi; int j = vi; that reads vi twice will result in i == j which will not be true if vi is a hardware counter for example).

2
3

The volatile keyword is intended to prevent the compiler from applying any optimisations on objects that can change in ways that cannot be determined by the compiler.

Objects declared as volatile are omitted from optimisation because their values can be changed by code outside the scope of current code at any time. The system always reads the current value of a volatile object from the memory location rather than keeping its value in temporary register at the point it is requested, even if a previous instruction asked for a value from the same object.

Consider the following cases

1) Global variables modified by an interrupt service routine outside the scope.

2) Global variables within a multi-threaded application.

If we do not use volatile qualifier, the following problems may arise

1) Code may not work as expected when optimisation is turned on.

2) Code may not work as expected when interrupts are enabled and used.

Volatile: A programmer’s best friend

https://en.wikipedia.org/wiki/Volatile_(computer_programming)

1
  • The link you posted is extremely outdated and doesn't reflect current best practices. Commented Oct 5, 2016 at 13:48
2

Beside the fact that the volatile keyword is used for telling the compiler not to optimize the access to some variable (that can be modified by a thread or an interrupt routine), it can be also used to remove some compiler bugs -- YES it can be ---.

For example I worked on an embedded platform were the compiler was making some wrong assuptions regarding a value of a variable. If the code wasn't optimized the program would run ok. With optimizations (which were really needed because it was a critical routine) the code wouldn't work correctly. The only solution (though not very correct) was to declare the 'faulty' variable as volatile.

3
  • 3
    It is a faulty assumption the idea that the compiler doesn't optimize access to volatiles. The standard knows nothing about optimizations. The compiler is required to respect what the standard dictates, but it is free to do any optimizations that don't interfere with the normal behavior.
    – Terminus
    Commented Sep 25, 2008 at 10:22
  • 3
    From my experience 99.9% of all optimization "bugs" in gcc arm are errors on the part of the programmer. No idea if this applies to this answer. Just a rant on the general topic Commented Dec 31, 2013 at 0:11
  • 1
    @Terminus "It is a faulty assumption the idea that the compiler doesn't optimize access to volatiles" Source?
    – curiousguy
    Commented Jul 15, 2018 at 4:21
2

Your program seems to work even without volatile keyword? Perhaps this is the reason:

As mentioned previously the volatile keyword helps for cases like

volatile int* p = ...;  // point to some memory
while( *p!=0 ) {}  // loop until the memory becomes zero

But there seems to be almost no effect once an external or non-inline function is being called. E.g.:

while( *p!=0 ) { g(); }

Then with or without volatile almost the same result is generated.

As long as g() can be completely inlined, the compiler can see everything that's going on and can therefore optimize. But when the program makes a call to a place where the compiler can't see what's going on, it isn't safe for the compiler to make any assumptions any more. Hence the compiler will generate code that always reads from memory directly.

But beware of the day, when your function g() becomes inline (either due to explicit changes or due to compiler/linker cleverness) then your code might break if you forgot the volatile keyword!

Therefore I recommend to add the volatile keyword even if your program seems to work without. It makes the intention clearer and more robust in respect to future changes.

2
  • Note that a function can have its code inlined while still generating a reference (resolved at link time) to the outline function; this will be the case of a partially inlined recursive function. A function could also have its semantic "inlined" by the compiler, that is the compiler assumes the side effects and result are within the possible side effects and results possible according to its source code, while still not inlining it. This is based on the "effective One Definition Rule" which states that all definitions of an entity shall be effectively equivalent (if not exactly identical).
    – curiousguy
    Commented Jan 7, 2019 at 21:30
  • Avoiding portably the inlining of a call (or "inlining" of its semantic) by a function whose body is visible by the compiler (even at link time with global optimization) is possible by using a volatile qualified function pointer: void (* volatile fun_ptr)() = fun; fun_ptr();
    – curiousguy
    Commented Jan 7, 2019 at 21:32
2

In the early days of C, compilers would interpret all actions that read and write lvalues as memory operations, to be performed in the same sequence as the reads and writes appeared in the code. Efficiency could be greatly improved in many cases if compilers were given a certain amount of freedom to re-order and consolidate operations, but there was a problem with this. Even though operations were often specified in a certain order merely because it was necessary to specify them in some order, and thus the programmer picked one of many equally-good alternatives, that wasn't always the case. Sometimes it would be important that certain operations occur in a particular sequence.

Exactly which details of sequencing are important will vary depending upon the target platform and application field. Rather than provide particularly detailed control, the Standard opted for a simple model: if a sequence of accesses are done with lvalues that are not qualified volatile, a compiler may reorder and consolidate them as it sees fit. If an action is done with a volatile-qualified lvalue, a quality implementation should offer whatever additional ordering guarantees might be required by code targeting its intended platform and application field, without requiring that programmers use non-standard syntax.

Unfortunately, rather than identify what guarantees programmers would need, many compilers have opted instead to offer the bare minimum guarantees mandated by the Standard. This makes volatile much less useful than it should be. On gcc or clang, for example, a programmer needing to implement a basic "hand-off mutex" [one where a task that has acquired and released a mutex won't do so again until the other task has done so] must do one of four things:

  1. Put the acquisition and release of the mutex in a function that the compiler cannot inline, and to which it cannot apply Whole Program Optimization.

  2. Qualify all the objects guarded by the mutex as volatile--something which shouldn't be necessary if all accesses occur after acquiring the mutex and before releasing it.

  3. Use optimization level 0 to force the compiler to generate code as though all objects that aren't qualified register are volatile.

  4. Use gcc-specific directives.

By contrast, when using a higher-quality compiler which is more suitable for systems programming, such as icc, one would have another option:

  1. Make sure that a volatile-qualified write gets performed everyplace an acquire or release is needed.

Acquiring a basic "hand-off mutex" requires a volatile read (to see if it's ready), and shouldn't require a volatile write as well (the other side won't try to re-acquire it until it's handed back) but having to perform a meaningless volatile write is still better than any of the options available under gcc or clang.

1

One use I should remind you is, in the signal handler function, if you want to access/modify a global variable (for example, mark it as exit = true) you have to declare that variable as 'volatile'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.