Page 1 of 1
Passing multi-dimensioned array pointer
Posted: 18 September 2009, 12:31 PM
by GTBecker
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?
Posted: 18 September 2009, 15:32 PM
by mikep
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.
Posted: 18 September 2009, 15:51 PM
by dkinzer
The thread that Mike referred to contains several different examples. One that I think matches your requirement is shown below.
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
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.
Posted: 18 September 2009, 16:51 PM
by GTBecker
Thanks, gentlemen. That'll do it.
Posted: 18 September 2009, 18:57 PM
by GTBecker
Indeed, the Based method works fine for me. Thanks, again.
Posted: 20 September 2009, 17:27 PM
by GTBecker
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] = ...
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
}
Any hint there?
Posted: 20 September 2009, 18:57 PM
by mikep
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".
As I reported earlier in this thread:
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.
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.
Posted: 21 September 2009, 4:44 AM
by GTBecker
Sorry, Mike; I remembered that there was a mention of native mode compile there somewhere but looked for it in the thread you referred to, not your post. I'll wait for Don's examination. Thanks.
Posted: 23 September 2009, 13:06 PM
by dkinzer
GTBecker wrote:I'll wait for Don's examination.
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.
I'll post a link to an experimental version containing the correction in a few days.
Posted: 30 September 2009, 13:06 PM
by dkinzer
dkinzer wrote:I'll post a link to an experimental version containing the correction in a few days.
This issue has been corrected in an experimental release of the compiler:
http://www.zbasic.net/download/ZBasic/2 ... 2-8-10.zip
Posted: 30 September 2009, 18:42 PM
by GTBecker
Excellent. The technique appears to work fine on the ZX-24n now. Thanks, Don.