Array initialisation. Size error.

Any bugs you encounter with Flowcode should be discussed here.
Post Reply
mnfisher
Valued Contributor
Posts: 953
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Flowcode v10 Array initialisation. Size error.

Post 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 53 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

Steve-Matrix
Matrix Staff
Posts: 1253
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Array initialisation. Size error.

Post 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.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Array initialisation. Size error.

Post 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

Steve-Matrix
Matrix Staff
Posts: 1253
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Array initialisation. Size error.

Post 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).

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Array initialisation. Size error.

Post 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

Steve-Matrix
Matrix Staff
Posts: 1253
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Array initialisation. Size error.

Post by Steve-Matrix »

True. But Flowcode was originally developed around a cut-down compiler that did not have "sizeof" and had other limitations.

Post Reply