C
# C Programming
# Special keywords
# Volatile
|
|
Volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time-without any action being taken by the code the compiler finds nearby.
# Use in peripheral registers
These registers may have their values changed asynchronously during program flow. Code without this keyword can be optimised by the compiler into an infinite loop.
|
|
Compiler interprets the ptr value is being always 0, as it has already loaded the value in the second line, resulting in an infinite loop:
|
|
Same situations can occur for variables that may be modified in ISRs or by multi-threaded applications.