Optimized away
-On
switches.
a
in the following
example may cause this behavior.
int main() { int a = 0; while (a < 42) { a += 2; } }
The reason for a
to be optimized away is obvious
as the incrementation of a
does not affect any other part of our
code. This example of a busy-wait loop is a prime example of unexpected behavior
if you are unaware of this fact.
a
as volatile
. Other
situations where a variable should be declared volatile is if some variable is shared
between the code and an ISR1.For a thorough walkthrough of this issue, have a look at Cliff Lawson’s excellent tutorial on this issue.
Interrupt Service Routine