Weird compiler behavior

So I’m programming away with my code usage sitting around 80%. Suddenly my next compile shows it drop to 24%.

Tracking it down, it was a bug in this line:

for (byte castIndex = 0; castIndex < MAX_CASTS_PER_TILE; castIndex)

If you see the problem, I forgot to increment my loop counter. Adding the ++ fixed it. Not sure what the compiler was thinking

I did not try to figure out exactly what happened yet but based on the side effect, this typo caused a. considerable chunk of your code to become unreachable. Was this loop around most of the code in your program (including function calls, of course)?

1 Like

Nope, just a simple for loop within a function. Adding the ++ shouldn’t affect scoping at all.

It is not related to scoping at all. The error resulted in the compiler determining a lot of code was unreachable so it simply removed it.

In fact, what is happening here is actually pretty easy to explain. Without the ++, the loop is an infinite one. So the compiler knows that once it reaches it, no code that was not used before it will ever be executed so it simply elided all that code.