Passing multi-dimensioned array pointer
Passing multi-dimensioned array pointer
I see that I cannot pass ByRef a 2x4 array of singles (a small Kalman covariance matrix), like
Sub Foo(ByRef Matrix(,) as single)
X = Matrix(1,2)
Can I pass a defined pointer and index the array in the sub, like
Sub Foo(ByVal MatrixPointer as something)
X = [MatrixPointer](1,2)
or something similar?
Sub Foo(ByRef Matrix(,) as single)
X = Matrix(1,2)
Can I pass a defined pointer and index the array in the sub, like
Sub Foo(ByVal MatrixPointer as something)
X = [MatrixPointer](1,2)
or something similar?
Tom
See here for the explanation of how to modify multi-dimensional array values inside a function or subroutine: http://www.zbasic.net/forum/about809.html .It works by passing the address of the array and then define in the routine an array that is BASED on that address.
The example code compiles fine for ZVM devices. I do not have a ZBasic device handy to test the code on a ZVM device but it should work. The example code does not compile correctly for native mode devices. It looks like a back-end code generation problem that is creating incorrect code for the C compiler. We will probably need Don to investigate this.
The example code compiles fine for ZVM devices. I do not have a ZBasic device handy to test the code on a ZVM device but it should work. The example code does not compile correctly for native mode devices. It looks like a back-end code generation problem that is creating incorrect code for the C compiler. We will probably need Don to investigate this.
Mike Perks
The thread that Mike referred to contains several different examples. One that I think matches your requirement is shown below.Note that there is some risk that the block of memory whose address is passed to the subroutine could be accessed in a manner that is incompatible with the original definition if the dimensions and/or type in the Based definition do not match those of the original array. The risk could be reduced somewhat by using defined constants for the array dimensions.
Code: Select all
Sub Main()
Dim a(1 to 4, 1 to 10) as Single
Call foo(a.DataAddress)
End Sub
Sub foo(ByVal addr as UnsignedInteger)
Dim d(1 to 4, 1 to 10) as Single Based addr
<code to access the array here>
End Sub
- Don Kinzer
Well, the Based method works fine on a ZX-24a but the back-end compile fails for the ZX-24n: "...c.105: error: subscripted value is neither array nor pointer".
I'm looking at the .c file. Line 105 is the first zv_NewCovars[1 - 1] = ...
Any hint there?
I'm looking at the .c file. Line 105 is the first zv_NewCovars[1 - 1] = ...
Code: Select all
void
zf_KalmanPredict(float *zp_RawGyro, float *zp_Rate, float *zp_Angle, float *zp_ProcessNoise, float *zp_GyroNoise, float *zp_Bias, float zp_dT, uint16_t zp_CovarsAddr)
{
float zv_NewCovars[4];
#define zv_Covars ((float *)(zp_CovarsAddr))
*zp_Rate = *zp_RawGyro - *zp_Bias;
*zp_Angle = *zp_Angle + (*zp_Rate * zp_dT);
zv_NewCovars[1 - 1] = (*zp_ProcessNoise - zv_Covars[2 - 1][1 - 1]) - zv_Covars[1 - 1][2 - 1];
zv_NewCovars[2 - 1] = -zv_Covars[2 - 1][2 - 1];
zv_NewCovars[3 - 1] = -zv_Covars[2 - 1][2 - 1];
zv_NewCovars[4 - 1] = *zp_GyroNoise;
zv_Covars[1 - 1][1 - 1] = zv_Covars[1 - 1][1 - 1] + (zv_NewCovars[1 - 1] * zp_dT);
zv_Covars[2 - 1][1 - 1] = zv_Covars[2 - 1][1 - 1] + (zv_NewCovars[2 - 1] * zp_dT);
zv_Covars[1 - 1][2 - 1] = zv_Covars[1 - 1][2 - 1] + (zv_NewCovars[3 - 1] * zp_dT);
zv_Covars[2 - 1][2 - 1] = zv_Covars[2 - 1][2 - 1] + (zv_NewCovars[4 - 1] * zp_dT);
#undef zv_Covars
}
Tom
As I reported earlier in this thread:GTBecker wrote:Well, the Based method works fine on a ZX-24a but the back-end compile fails for the ZX-24n: "...c.105: error: subscripted value is neither array nor pointer".
I did send a followup email to Don and he said he would investigate it. I do not have any further update at this point.mperks wrote:The example code does not compile correctly for native mode devices. It looks like a back-end code generation problem that is creating incorrect code for the C compiler. We will probably need Don to investigate this.
Mike Perks
Mike reminded me of this several days ago. I now remember having seen the problem report but I had forgotten about it in the interim. The problem is in the code generated to implement the based variable (using a #define) but it only occurs when the based variable has more than one dimension. I was able to correct the problem using a slightly more complicated strategy for the multi-dimensional case but it appears to be working satisfactorily.GTBecker wrote:I'll wait for Don's examination.
I'll post a link to an experimental version containing the correction in a few days.
- Don Kinzer
This issue has been corrected in an experimental release of the compiler:dkinzer wrote:I'll post a link to an experimental version containing the correction in a few days.
http://www.zbasic.net/download/ZBasic/2 ... 2-8-10.zip
- Don Kinzer