user79800: I am trying to run a very simple program to glow LEDs in a certain fashion, I connected 9 LEDs to the GPIOs or digital pins, 2 To 10, via 220 Ohm resistors; when I am running the program it is hanging the Arduino after some time interval and LEDs stops glowing, not sure if it is a hardware issue or some bug in the software/code.
Here is the code:
int outGPIOsarr[9] = {2,3,4,5,6,7,8,9,10};

void setup() {
  
  for(int i=0; i<10; i++)
pinMode(outGPIOsarr[i], OUTPUT);
}

void loop() {
  
for(int i=0; i<10; i++) 
 {
digitalWrite(outGPIOsarr[i],HIGH);
delay(10);
 }

for(int i=0; i<10; i++) 
 {
digitalWrite(outGPIOsarr[i],LOW);
delay(10);
 }

}


user14490: This is the UB!
The indexes in array in C++ counts from 0;
So, the last index in your array of 9 elements is 8.
All cycles must stop at 8. Yours are stops at 9.
Access beyond the existing element in the array is the Undefined Behavior - it hangs the MCU.

79800: <p>I am trying to run a very simple program to glow LEDs in a certain fashion, I connected 9 LEDs to the GPIOs or digital pins, 2 To 10, via 220 Ohm resistors; when I am running the program it is hanging the Arduino after some time interval and LEDs stops glowing, not sure if it is a hardware issue or some bug in the software/code.</p>
<p>Here is the code:</p>
<pre><code>int outGPIOsarr[9] = {2,3,4,5,6,7,8,9,10};

void setup() {
  
  for(int i=0; i&lt;10; i++)
pinMode(outGPIOsarr[i], OUTPUT);
}

void loop() {
  
for(int i=0; i&lt;10; i++) 
 {
digitalWrite(outGPIOsarr[i],HIGH);
delay(10);
 }

for(int i=0; i&lt;10; i++) 
 {
digitalWrite(outGPIOsarr[i],LOW);
delay(10);
 }

}
</code></pre>
 14490: <p>This is the UB!</p>
<p>The indexes in array in <code>C++</code> counts from <code>0</code>;</p>
<p>So, the last index in your array of <code>9</code> elements is <strong><code>8</code></strong>.</p>
<p>All cycles must stop at <strong><code>8</code></strong>. Yours are stops at <code>9</code>.</p>
<p>Access beyond the existing element in the array is the Undefined Behavior - it hangs the MCU.</p>
