Content originally posted in LPCWare by sixten11 on Thu Nov 12 12:24:15 MST 2015
Quote: djrose
Yes. A simple optimization most compilers can do is look at the logic flow of a program. If it thinks that a program loop is not writing to a variable, it can simply read once into a register and then keep testing the register (/copy) instead of reading from a memory location.
But, of course, if you have some other action or task that is writing to the variable, you need to give a hint to the compiler to let it know that something else might access it. That is what the volatile basically does. It tells the compiler to re-read the variable from memory each time it is accessed within your program.
Your original loop with the sprint would normally work because the compiler loses track of the variable it read into a register, and next time it accesses the variable, it re-reads it from memory.
Simples :bigsmile:
Amazing :) thank you very much!