If something outside the current region or any subsequent child region (think: function calls) can change the variable that you are working in (there is a timer interrupt that will increase your variable, you pointed to var for some other code that can do something in response to an interrupt, etc.), then the variable must be declared mutable.
volatile is a hint to the compiler that says, "something else can change this variable." and the compiler’s answer: “Oh, I never trust a copy of this variable that I have in the register or on the stack. Every time I need to use this variable, I will read it from memory, because my copy in the register may be deprecated "
By announcing that everything is volatile, your code will significantly slow down and lead to a significant increase in binary code. Instead, the correct answer is to understand what needs to be marked by volatility, why this is so and appropriately labeled.
Chris raplee
source share