Beginner: for loop with warning "Waggressive-loop-optimizations"

Hello,

I'm new to the arduino project but it makes a lot of fun so far.

I get a warning because of a for-loop but I don't know why. It seems the compiler doesn't like the condition in the for-loop. If I put there index < 6 it works but index < pin_count doesn't.

This here works without warning:

const int pin_array[] = {11, 10, 9, 6, 5, 3};                    // LED Pins mit PWM
const int pin_count = sizeof(pin_array);                         // Anzahl von Pins


void setup ()
{
  for(int index = 0; index < 6; index++)           
  {
    pinMode(pin_array[index], OUTPUT);        // Pins im Array als Ausgabe-Pins defnieren
  }
}


void loop()
{

}

But this results in a warning:

const int pin_array[] = {11, 10, 9, 6, 5, 3};                    // LED Pins mit PWM
const int pin_count = sizeof(pin_array);                         // Anzahl von Pins


void setup ()
{
  for(int index = 0; index < pin_count; index++)           
  {
    pinMode(pin_array[index], OUTPUT);        // Pins im Array als Ausgabe-Pins defnieren
  }
}


void loop()
{

}

The warning:

...\programm-2.ino:20:28: warning: iteration 6u invokes undefined behavior [-Waggressive-loop-optimizations]

     pinMode(pin_array[index], OUTPUT);        // Pins im Array als Ausgabe-Pins defnieren

                            ^
const int pin_array[] = {11, 10, 9, 6, 5, 3};                    // LED Pins mit PWM
const int pin_count = sizeof(pin_array);

pin_count is the number of bytes in pin_array, not the number of elements.

const int pin_array[] = {11, 10, 9, 6, 5, 3};                    // LED Pins mit PWM
const int pin_count = sizeof(pin_array) / sizeof (pin_array [0]);

inob:
But this results in a warning:

Yes, silly code meets a clever compiler ==> warning about silly things happening in the code.

The compiler detects that something is wrong with this:

const int pin_count = sizeof(pin_array);                         // Anzahl von Pins

So you better fix the comment to tell what the code does:

const int pin_count = sizeof(pin_array);                         // number of bytes in the pin array

(but your sketch will not work due to buffer overflow while accessing the array)

Or fix the code to actually do what the comment tells about:

const int pin_count = sizeof(pin_array)/sizeof(int);       // Anzahl von Pins

const int pin_array[] <<-- why not byte?
const int pin_count <<-- why not byte?
Are the pin numbers going to exceed 255? Is the pin count going to exceed 255?

Change this:

const int pin_count = sizeof(pin_array);

to this:

const int pin_count = sizeof(pin_array) / sizeof(int);

Regards,
Ray L.

A slightly more flexible way to write AWOL's solution is:

#define ELEMENTCOUNT(x)  (sizeof(x) / sizeof(x[0]))

This will work with all data types (e.g., char, int, long, etc.) because the macro doesn't state the specific data type, but the compiler figures it out the type from the definition of the parameter x, so you might use it as:

  for(int index = 0; ELEMENTCOUNT(pin_array); index++)

You mean:

  for(int index = 0; index < ELEMENTCOUNT(pin_array); index++)

Yep...

AWOL:
pin_count is the number of bytes in pin_array, not the number of elements.

Thank you a lot.

That was quite a stupid mistake but thanks for alle the answers. You guys are great.

It's a perfectly understandable mistake, as is the one you'll make if you try to use the same trick inside a function, on function parameter that happens to be a pointer :wink: