Array contents in debugger

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

Flowcode v10 Array contents in debugger

Post by mnfisher »

This is very strange - my program gives the same error 'reliably' - when stepping through it.

I've been able to replicate in a code snippet.

The debugger gives some 'strange' values for an array:
array.jpg
array.jpg (52.35 KiB) Viewed 809 times
When passing an array that is 'smaller' than the parameter of a macro - so in the image I'm passing an 8 byte array to a macro where the parameter is defined as x[10] (note that I pass in variable length arrays) - the code works correctly - just some 'visual' noise.

Note it would also be good if the top line displayed B[10] instead of just []data[10] - type shows on expansion though.
array disp.fcfx
(9.94 KiB) Downloaded 46 times
Is a a (slightly) more complicated that it needs to be example.

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 contents in debugger

Post by Steve-Matrix »

Thanks, Martin. I will look into this.

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

Re: Array contents in debugger

Post by Steve-Matrix »

Unfortunately, there is no easy fix to this issue. But it might help if I explain what is going on.

When a function in C takes an array as a parameter, it is actually passed into the function as a pointer to the first array member. It is passed "by reference" and any action on this parameter variable within the function actually acts on the variable from the calling function. The parameter does not contain any size info and so the function does not know the size of the array being passed in. For example:

Code: Select all

void main()
{
  myarr[5] = {1, 2, 3, 4, 5};
  myfunct(myarr);
  //at this point, myarr = {25, 2, 3, 4, 5}
}

myfunct(myparam[])
{
  myparam[0] = 25;  //this changes the actual array passed in to the function
  
  //myparam[8] = 25;
  //the above is legal C (because this function cannot know the size of the array being passed in), 
  //but will write to memory beyond the array limits and cause odd behaviour or crash the program.
}
In Flowcode, array parameters to macros (i.e. functions) need to be declared with a size, even though this size is not necessarily the size of the variable being passed into the macro. This size represents the expected size of a variable being passed in to the macro and allows for some design-time errors to be trapped.

The attached project should help explain. It's similar to your project, but the variable being passed in is now a global variable and you will be able to see how that is changed if you step through the program.

In effect, the parameter 'a' becomes the passed in variable 'x'. In C there are no inherent checks on array bound access, but Flowcode does some checks. However, when it comes to array parameters the 'check' occurs at runtime while the program is simulating.

To avoid the problem, always ensure the dimensions of the array parameter match the dimensions of the array being passed in. There may be times when this is impossible, in which case be aware of this issue and make sure you always access within the bounds on the array being passed into the macro.
Attachments
arrays as params.fcfx
(9.34 KiB) Downloaded 44 times

Post Reply