Page 1 of 1

Array initialisation. Size error.

Posted: Wed Aug 16, 2023 7:54 pm
by mnfisher
Using a lookup table - I noticed a bug when my code worked in simulation but not on MCU. I used an Arduino Uno here and a 256 word lookup table. In my 'test' program to demonstrate (and to try and pinpoint the error in my main code) - it worked until I added a .return = value statement. Then stack corruption occurs and the program crashes.

Looking at the C code generated - (from constant table[256])

Code: Select all

#define FCLsz_TABLE 2017
  const MX_UINT16 FCL_TABLE[FCLsz_TABLE] = {0x0000, 0x8005,
  
The 2017 is incorrect (and eats all the available RAM)
Changing the table size to 2 gives a generated table size of 14.
data.fcfx
(13.16 KiB) Downloaded 293 times
It works correctly using variable instead of creating a constant.

As an aside - I initially missed a 0 from a hex number in the lookup (so x0110 instead of 0x0110) this simulated fine but (using 0 for the duff value) but fails to compile.
Similarly I can add a string as a value (say {0x1110, "abc"} - which compiles okay (it's #define data) but is a little odd?

Martin

Re: Array initialisation. Size error.

Posted: Thu Aug 17, 2023 8:30 am
by Steve-Matrix
Thanks, Martin.

It looks like there is an issue with local constant arrays. The issue is not present with local variables, global variables or global constants. I will investigate further and try to fix.

As a workaround, I suggest you define it as a global constant. That should not impact memory differently to a local constant.

Re: Array initialisation. Size error.

Posted: Thu Aug 17, 2023 9:26 am
by mnfisher
Thanks Steve,

The table is either destined to be in eeprom or a variable and calculated in the initialisation - but was doing some testing en route

Maroon

Re: Array initialisation. Size error.

Posted: Thu Aug 17, 2023 9:37 am
by Steve-Matrix
I've found the source of the issue and I will fix in a future release of Flowcode.

The ability to create constant arrays was a newly added feature in v10. Previous Flowcode versions allowed only numbers or string constants. The incorrect value for the array definition for local constant arrays comes because the code is expecting a string constant and uses the length of the text in the constant value (as if it were a string of characters).

Re: Array initialisation. Size error.

Posted: Thu Aug 17, 2023 2:15 pm
by mnfisher
Thanks,

Would it be easier to generate something like:

const MX_UINT16 FCL_TABLE[] = {1,2,3...} ;
const MX_UINT16 FCLsz_TABLE = sizeof (FCL_TABLE) ;

And let the compiler handle some of the calculations?

Martin

Re: Array initialisation. Size error.

Posted: Thu Aug 17, 2023 5:05 pm
by Steve-Matrix
True. But Flowcode was originally developed around a cut-down compiler that did not have "sizeof" and had other limitations.