Hi,
Yesterday I've spent more than two hours trying to figure out why a piece of code will compile with no errors but not run at all.
I was playing with a SD card sector reader / writer code and it happened I wanted to make a 512 elements array with some data in order to check when reading the SD card. For this I've wrote inside main:
int array1[520] int k for (k=0;k<512; k++) { array1[k] = k; // see text below array1[512-k]=k; }
This compiled OK, however the execution stops completely at the beginning of the for loop. If I commented out the line see text below above, the execution was normal.
If I defined int array1[520] out of the main (global) the program would not run at all.Or make weird things as looping the code between the beginning of the main up to this point. Like this point was a GOTO main() again.
After more than an hour of changing names even the MCU ( ATMega168). I decided to decrease the size of the array to 20 and then everything worked OK.
So, it happens to be that there was another 520 elements array declared in the code, so ( apparently) the atmega168 memory was two small to hold both arrays.
What I do not understand is why the compiler or the linker did not notice this problem and produces and hex file which will not work.
I have no clue on how the compiler deals with the FLASH, EEPROM and RAM . I think that the variables reserve RAM only ?
I believe that the solution here should be to use PROGMEM to store the array. But in this case, the values can not be changed at run time right?
Anyway, i would like to see what people with much more knowledge than me can comment on this . Anyway, please remember this post when you are having code doing unexplainable things!!
Jose