[-Waggressive-loop-optimizations] compile error

Hi,
I get this very strange compilation error: [-Waggressive-loop-optimizations]
All libraries used for this code are up to date and are being used, no doubles of these libraries anywhere.
It seems to point to a function called all2ZeroSpeed, and yes the speed is “Speeed”.

The code is in attachment (too large to include in `` )

Can you please give some pointers? Any help is very much appreciated.

The extended compilation output gives following lines in red:

C:\Users\Mysafety\Google Drive\documents\Arduino\David_Bodnar_handheld_DCC___throttle\David_Bodnar_handheld_DCC___throttle.ino: In function ‘all2ZeroSpeeed’:
C:\Users\Mysafety\Google Drive\documents\Arduino\David_Bodnar_handheld_DCC___throttle\David_Bodnar_handheld_DCC___throttle.ino:366:37: warning: iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations]
** Serial.print(LocoAddress[tempx] );//locoID);**
** ^**
C:\Users\Mysafety\Google Drive\documents\Arduino\David_Bodnar_handheld_DCC___throttle\David_Bodnar_handheld_DCC___throttle.ino:364:3: note: containing loop
** for (int tempx = 0; tempx <= maxLocos; tempx++) {**
** ^**

David_Bodnar_handheld_DCC___throttle.ino (12.9 KB)

for (int tempx = 0; tempx <= maxLocosI can’t see your code, but from the error messages, that comparison looks a bit odd to me.

Hi AWOL,

this is the section of the code that the error refers to:

void all2ZeroSpeeed() {  // set flag to 1 to stop, set to 0 to restore
  for (int tempx = 0; tempx <= maxLocos; tempx++) {
    Serial.print("<t1 ");
    Serial.print(LocoAddress[tempx] );//locoID);
    Serial.print(" ");
    if (ZeroSpeedFlag == 1) {
      Serial.print(0);//LocoSpeed[0] );
    }
    else Serial.print(LocoSpeed[0] );
    Serial.print(" ");
    Serial.print(LocoDirection[1] );
    Serial.write(">");
  }

AWOL:
for (int tempx = 0; tempx <= maxLocosI can’t see your code, but from the error messages, that comparison looks a bit odd to me.

Hi AWOL, is there a mistake in that statement?

brice3010: Hi AWOL, is there a mistake in that statement?

Maybe, maybe not.

If I have six locos, I would expect maxLocos to have the value six, but I would not expect a for loop to count up to six, especially if I'm using tempx as an index into an array that has maxLocos elements.

From the ino

int maxLocos = 4;// number of loco addresses
int LocoAddress[4] = {1830, 3, 999, 4444};

Arrays run from 0. So your For loop should use < not <=

Which may or may not be the compilers problem.

Mark

I do apologise - the message (and the value of maxLocos) was there in the error messages iteration 4 invokes undefined behavior

SOLVED!!

Arrays run from 0. So your For loop should use < not <=

That did it; thanks a lot AWOL and holmes4: you are geniuses!!