PROGMEM - wrong data read

I wrote an Arduino sketch which uses a relative large number of byte and int arrays, stored into flash memory with PROGMEM. (Arduino 1.6.5)

However the behavior seems for me strange and I don’t have any explanation.

In the first picture you can see that, by directly giving a value to the “index” variable (currentPrg) the compiled size is aprox. 6.5KB and the code works perfect. (despite weird reported size as I have more than 6.5kB of array data, beside the code… but anyway, it works!)

Now, if I obtain the “index” value from another trivial function (very simple, it works, I double checked!), compile size suddenly becomes 28.5kB and the returned array values are wrong (many of them are 0xFF or 0xFFFF).
As I said, the function used to obtain the index is very simple and works just fine and is not responsive(?) for the big difference on compiled size.
Usage of this function to obtain the index is THE ONLY difference between the 2 versions of the code.

Please let me know if you have any idea.
I can attach other parts of the code if needed.

Thank you!

I can attach other parts of the code if needed.

On the other hand you can attach ALL of your code if you expect ANY help. Your choice.

After some more diggings I found that read sequence wasn’t OK.
This way is correct:

  currentPrg = selectProgram();

  nrRepetari = pgm_read_byte((uint8_t*)pgm_read_word(&(nrRepetari_table[currentPrg])));
  for(i = 0 ; i < nrRepetari ; i++) {
    turatie_motor_0[i] = pgm_read_byte((uint8_t*)pgm_read_word(&(turatie_motor_0_table[currentPrg])) + i);
    turatie_motor_1[i] = pgm_read_byte((uint8_t*)pgm_read_word(&(turatie_motor_1_table[currentPrg])) + i);
    pozitie_X[i] = pgm_read_byte((uint8_t*)pgm_read_word(&(pozitie_X_table[currentPrg])) + i);
    pozitie_Z[i] = pgm_read_byte((uint8_t*)pgm_read_word(&(pozitie_Z_table[currentPrg])) + i);
    pozitie_S[i] = pgm_read_byte((uint8_t*)pgm_read_word(&(pozitie_S_table[currentPrg])) + i);
    temporizare0[i] = pgm_read_word((uint16_t*)pgm_read_word(&(temporizare0_table[currentPrg])) + i);
    temporizare2[i] = pgm_read_word((uint16_t*)pgm_read_word(&(temporizare2_table[currentPrg])) + i);
  }

The increased size of compiled code was a result of uploading ALL the static arrays when the index is not fixed.
When it has already a value at compile time, only corresponding array is actually uploaded… smart compiler. :slight_smile:

I can't read the original code ( screen too small ) but it sounds like you were creating
a multi dimensional array.
These use a lot of space.
Dwight

dwightthinker:
I can't read the original code ( screen too small ) but it sounds like you were creating
a multi dimensional array.
These use a lot of space.

The dimensions do not matter, it is the number of elements (and their size) that fill the memory.

Put superstition away while programming.

dwightthinker:
I can't read the original code ( screen too small ) but it sounds like you were creating
a multi dimensional array.
These use a lot of space.
Dwight

Kind of multidimensional array: each of my "program" (from a total of 30) consists on 7 unidimensional arrays with 80 elements each, byte or int.
If the index into this "multidimensional program" array is a fixed number at the compile time, only corresponding 7 array are loaded to flash.
On the other hand, if the index is unknown at the compile time (i.e. result of an user selection via some function) then all the 7 x 30 arrays are loaded to flash. That was the reason of the difference.

As I thought.
Dwight