Hi, i am rather new to the arduino IDE although i have some experience with programming other micros in the past.
while testing a few things i came across a strange problem of a loop's break condition not being recognized when i index an array outside its range.
At the moment the loop never stops although i think it should still do so.
The following changes make the loop work:
- increasing the Array size by at least 1 (from 20 to 21 in this example)
- changing the compare expression of the loop to "index <20"
- not accessing the array in the loop after its last value
- removing the break statement at the end of the if sentence
certainly i do know that trying to access an array outside its range is not so good, but i would like to understand what exactly happens here and makes the loop loose control of its break condition.
also i absolutely do not understand why removing the break statement in the loop could change anything at all.
I would be greateful for any insight in this problem.
int searchList[20];
void setup() {
int target = 5343; //some number
int index = 0;
Serial.begin(9600);
memset(searchList,0,sizeof(searchList)); //clear Array
for (index = 0; index <= 20;index++){
Serial.print(index); // Serial prints for debugging purposes
Serial.print(" --- ");
Serial.println(index <= 20); // checking loop exit condition, this usually comes out wrong!?!?!?
if (searchList[index] == target){
break;
}
}
Serial.print("Target not found");
}
void loop() {
}
Your for loop condition should be "< 20"
The memset is superfluous.
Warning... Blind leading the blind... I'm new (ish) too.
It's about how arrays work I think... If you set up a twenty item array
int array[20]
Then there is in fact no item array[20], because the first item is array[0], not array[1].
This means you almost always want to bound anything that itemised the array with "<", not "<=".
Wait until someone more knowledgeable than me confirms that though..!
Thanks for the quick reply.
You both certainly are correct, with the condition of "< 20" everything works.
My main concern is, what exactly happens when i use "<=20" or "<21" or anything else that leads to the following if statement to access the array outside its range. Why isn't the loop stopping anyway, shouldn't its break condition be satisfied independant of any array being accessed outside its range?
, what exactly happens when i use "<=20" or "<21" or
If you access anything out of the array, particularly if you write, all bets are off.
The break condition has very little to do with the bounds of the array, except that the preceding if condition accesses out of the array.
From what I understand, memory has to be allocated to an array to store the variables. Its like booking a room at a hotel... You tell it in advance what kind of room you need.
If you don't, then it tries to put your six foot tall guest in a baby's bed, and he gets all bent outta shape.
Why do you think the break condition is satisfied?
Your array is being cleared (incorrectly) at the beginning. And you compare its element to a number other than 0.
arduino_new:
Why do you think the break condition is satisfied?
Your array is being cleared (incorrectly) at the beginning. And you compare its element to a number other than 0.
The break condition i (most likely erroneously) think should be satisfied is simply given by the variable "index" getting bigger than 20 and the loop continues merrily although the condition is "index <=20"
The answer could be:
TheMemberFormerlyKnownAsAWOL:
If you access anything out of the array, particularly if you write, all bets are off.
Although i dont't get why that should be so. I would understand that i would get some bogus value when trying an out of range index of an arry (reading that is).
But i do not get why the loop is in anyway compromised because of that.
TheMemberFormerlyKnownAsAWOL:
?
Just an analogy... feel free to correct if it's wrong... the rooms are the array elements, the guests are the values. You have to get the right number [x] of the right sort (int, byte, long) of rooms ready before you put the guests in them, or things go wrong with your party (code)...
It just helps my non-programmer brain to think of it that way.
Well, i think i found the answer here:Accessing array out of bounds in C/C++ - GeeksforGeeks
In high level languages such as Java, there are functions which prevent you from accessing array out of bound by generating a exception such as java.lang.ArrayIndexOutOfBoundsException. But in case of C, there is no such functionality, so programmer need to take care of this situation.
What if programmer accidentally accesses any index of array which is out of bound ?
C don’t provide any specification which deal with problem of accessing invalid index. As per ISO C standard it is called Undefined Behavior.
An undefined behavior (UB) is a result of executing computer code whose behavior is not prescribed by the language specification to which the code can adhere to, for the current state of the program (e.g. memory). This generally happens when the translator of the source code makes certain assumptions, but these assumptions are not satisfied during execution.
Thank you all for your quick replies, it is good to know that if i happen to have a real problem, this forum is a pleasant place to look for help and if possible help others.
I (try to) help.
The middle two words are the problem...but then there are people on here who cannot help real beginners because, in effect, they know too much...
... (that's not a criticism) ...
... and occasionally, expect too much...
... (that is)...
D&R...
I have not checked with your code, but often the compiler will give a warning of a potential out-of-bounds array access if the IDE is set to show all warnings.