tags:

views:

526

answers:

5

Hi

I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed? How to use them in different way?

Thanks a lot.

+4  A: 

They are definitely NOT the same thing. Mutable interacts with const. If you have a const pointer, you normally could not change members. Mutable provides an exception to that rule.

Volatile, on the other hand, is totally unrelated to changes made by the program. It means that the memory could change for reasons beyond the control of the compiler, therefore the compiler has to read or write the memory address every time and can't cache the content in a register.

Ben Voigt
+2  A: 

mutable: The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.

volatile: The volatile keyword is an implementation-dependent modifier, used when declaring variables, which prevents the compiler from optimizing those variables. Volatile should be used with variables whose value can change in unexpected ways (i.e. through an interrupt), which could conflict with optimizations that the compiler might perform.

Source

kitchen
+7  A: 

A mutable field can be changed even in an object accessed through a const pointer or reference, or in a const object, so the compiler knows not to stash it in R/O memory. A volatile location is one that can be changed by code the compiler doesn't know about (e.g. some kernel-level driver), so the compiler knows not to optimize e.g. register assignment of that value under the invalid assumption that the value "cannot possibly have changed" since it was last loaded in that register. Very different kind of info being given to the compiler to stop very different kinds of invalid optimizations.

Alex Martelli
`volatile` objects could also be changed by processes not involving the CPU at all. For example, a bytes-received register in a communications peripheral can increment itself on the receipt of a byte (and this can even trigger an interrupt). Another example is a pending-interrupts flags register in a peripheral.
Mike DeSimone
Also, `volatile` doesn't only mean that the object can change outside the compiler's knowledge - it also means that writes to the object can't be eliminated by the compiler even if those writes appear to be useless. For example: `x = 1; x = 0;` if `x` is volatile, the compiler has to emit both write operations (which might be significant at the hardware level). However, for a non-volatile object, the compiler could chose not to bother with writing the `1` since it's never used.
Michael Burr
A: 

A variable marked mutable allows for it to be modified in a method declared const.

A variable marked volatile tells the compiler that it must read/write the variable every time your code tells it too (i.e. it cant optimize away accesses to the variable).

Kyle Lutz
+1  A: 

A crude but effective way of thinking of the difference is:

  • The compiler knows when a mutable object changes.
  • The compiler cannot know when a volatile object changes.
Jonathan Leffler
In that vein: `volatile` bytes_received, `mutable` reference_count.
Mike DeSimone