about the "volatile"

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I don't understand the variables difference between "volatile" and not "volatile", such as "volatile char" and "char". I appreciate your help.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Volatile means that the variable can change for no obvius reason to the compiler
so you instruct the compiler to actually read the contents of that variable
every time you use it in your program.
Otherwise the compiler does optimisation tricks and instead of reading the actual contents of the variable it replaces that with a copy (probably constant)
The compiler thinks " nobody is changing that variable so let's save ram and program memory by replacing it with a constant.

For example if you use that variable in an interrupt the program will not work untill
you declare that variable as volatile because the interrupt routine can change the value of the variable without the compiler knowing it.
Or it can be any hardware that interacts with the particular variable.

I hope that the above help's you.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much, Chris. It does help me greatly.

admin's test signature