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
. Another situation where a variable
may be declared volatile is if a variable is shared between the code and ISR1.For a thorough walkthrough of this issue, have a look at Cliff Lawson’s excellent tutorial on this issue.
Interrupt Service Routine