I'm very new to Arduino and have few experience with C++, so this might be a newbie thing, but this really puzzels me. The code below comes from a bigger project, but i reduced it to the part where it goes wrong, so it doesn't do anything useful.
const short ritBufferSize = 250; // 5s
int ritmeBuffer[ritBufferSize - 1]; //250 -> 5 seconden
int noisefloor = 6000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {} // give serial port time to start}
memset(ritmeBuffer, 0, sizeof(ritmeBuffer));
}
void loop() {
// put your main code here, to run repeatedly:
int j = 0;
int test = 0;
for (j = 0; j < ritBufferSize; j++) {
if (ritmeBuffer[j] > noisefloor) {
Serial.print("Signal detected ");
Serial.println(ritmeBuffer[j]);
test = 1;
break;
// j = ritBufferSize; //other way to jump to the end of the loop
}
}
Serial.println(j);
delay(5000);
}
Now, if i execute this code, the for loop does not stop when the condition 'j < ritBufferSize' is met, it keeps reading from the array past it's boundary and then the loop exits at some point when the value it reads that way happens to be above noisefloor. now, if i remove the 'break;' and replace it with ' j = ritBufferSize;'everything works fine. So i have a working option, but still i'm puzzled why it goes wrong with the break?
Trace through your declared buffer size and the calculated indices, I think you've allocated 249 elements, but are indexing 0-249, which writes one past the end.
But I've gotta go now, so I can't recheck.
I'm pretty sure i have this right. ritmeBuffer has a size of 250 elements, going from 0 to 249. the for loop should only run as long as the counter is lower then 250, so until 249.
When i omit the break, my code is actually running fine. I could leave it like that. The break is an optimalisation, as when one array element is above the nosiefloor, i don't need to check the rest. But my problem is, when i leave the break there, but the if condition triggering the break does not happen (so all values are below noisefloor), the for loop goes on after it's condition is reached. not just one step, which you could expect if my logic with the buffersize - 1 was wrong, but sometimes up to more then 800..
What also surprises me: arduino just allows you to read arrays far beyond the boundaries you defined? any other language i programmed in before would throw an error and/or just stop working..
ok, so i wrongly assumed initialising an array[n], it would contain n + 1 elements (including 0) - like it does in some other languages, but actually it goes from 0 to n-1. one lesson learned, thanks all!
I've been programming since the '70s and have used dozens of languages. I don't know ANY language that makes the array longer than the defined length when the length is explicitly defined. In c/c++ room for a trailing null is allocated ONLY when initializing an array of char with a quoted string, and the array length is NOT explicitly specified.
In any case, assuming one language is exactly like another is foolish, especially since a simple Google search will tell you in a few seconds how it actually works.
Most of the time we use PowerBasic at the place where i work. (not my choice - i know almost no one else is). There declaring 'DIM myArray(10)' you would end up with an array of 11 element and DIM myArray(Arraysize - 1) is a common idiom. afairc other basics i worked with occasionaly had a similar thing.
In basic you would dimension an array with the upper boundary, rather than size. The reason google didn't help me here, was at first my code worked well, despite this bug and i only got troubles adding other things, so i was looking at the wrong place