Arduino iteration invokes undefined behavior

[-Waggressive-loop-optimizations]

Arduino iteration invokes undefined behavior [-Waggressive-loop-optimizations]

My Case for Looping

In this article, I will show you various ways to fix the Arduino iteration 3 invokes undefined behavior [-Waggressive-loop-optimizations] error that was confusing me.

This happened when I coded Arduino in Visual Studio Code in PlatformIO.

Iterating over the following code, you may not find any significant problems with the Arduino IDE.

for (index = 0; index <= 3; index++)
  {
    if (val[index] > 0)
    {
      Serial.println(val[index]);
    }
  }

But I code in VSCode, warning occurs on the line if (val[index] > 0).

What really happened?

How to fix it

This happens because the compiler finds an oddity in the index<=3 section.

Arrays are counted from index 0.

For loops, recommend using “<” instead of “<=“.

You have to remove the “=” in the index<=3 line.

So the loop will look like the following;

for (index = 0; index < 3; index++)
  {
    if (val[index] > 0)
    {
      Serial.println(val[index]);
    }
  }

The warning will disappear. Hopefully this article is useful and have a nice day.

Read more:
> ERROR 1045 (28000) Access denied for user Ubuntu
> Error You must select an OS Virtual Manager
> error: Flatpak system operation not allowed for user
> Error undefined constant MYSQLI STORE RESULT

Similar Posts