Strange Struct Array Behavior

Hi All,

I'm seeing a strange behavior in a struct array I created. The code looks like below, when I input an index beyond what is formed in the instantiation I am getting a (1,0,0,0) result.

For example when I have 4 objects in the array (index 0-3), the 5th (non-existent) object (index 4) is (1,0,0,0). When I have 5 objects (index 0-4), the 6th (index 5) is (1,0,0,0). Any calls to an object beyond that is (0,0,0,0). I was expecting errors, nulls, etc, possibly all 0s, but definitely not (1,0,0,0).

Any insight or assistance in eliminating this behavior and/or creating a "default" or "doesn't exist" case would be appreciated.

Below is my struct and array instantiation and usage:

//Declare a struct to contain Directions
struct MotorDirection{
  int m1;
  int m2;
  int m3;
  int m4;
};

//Directions setup
MotorDirection forward = {1,1,1,1};
MotorDirection left = {-1,1,1,-1};
MotorDirection backward = {-1,-1,-1,-1};
MotorDirection right = {1,-1,-1,1};

//Compile Directions into an array
MotorDirection Directions[] {forward,left,backward,right};

//Print a string of all of the array values
void loop()
{
      printDirection(Directions[0], 0);
      printDirection(Directions[1], 1);
      printDirection(Directions[2], 2);
      printDirection(Directions[3], 3);
      printDirection(Directions[4], 4);
      printDirection(Directions[5], 5);
  }

//Parse out a string to give feedback on the array values
void printDirection(MotorDirection md, int index)
{
      Serial.print("Index: ");
      Serial.print(index);
      Serial.print(", Motor Directions: ");
      Serial.print(md.m1);
      Serial.print(", ");
      Serial.print(md.m2);
      Serial.print(", ");
      Serial.print(md.m3);
      Serial.print(", ");
      Serial.println(md.m4);
}

if you have just 5 instances just print 5. If you print out of the boundaries you get "any" result.

btw: range based for loop comes very handy when dealing with arrays.

if you overrun arrays you get unpredictable behaviour depending on what memory locations are corrupted
programs may give incorrect results, crash immediatly, run for years without problems then one day with a different data set crash
in C++/Java/etc use vectors or similar containers with bounds checking (in fact Java arrays have built in bounds checking to prevent the type of problems you identified)

Why do you expecting almost anything, but not (1,0,0,0) ? :slight_smile: Is this value has specific meaning for you?

If you try to print array element beyond limits, you do not get a random result. On the contrary, you will get strictly defined value - contains of the memory address just after array end.

Don't go beyond the bounds of the array... simple.

Not sure if serious… When you access out of bounds you read random memory value, it’s called undefined behaviour, there is nothing strange about that

Simple solution, have the function check for an out of bounds value.

Thanks all, it totally makes sense that I should have an out of bounds check... I'm just new to coding in this environment that I was expecting it to be a blank (as I could add add'l objects) or err'd case (as I think VS would throw an error). I'll just put in that check... much appreciation!