Forum Index
Passing multi-dimensioned array pointer

 
Author Message
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 18 September 2009, 19:31 PM    Post subject: 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?
Back to top
mikep



Joined: 24 Sep 2005
Location: Austin, TX

Posted: 18 September 2009, 22:32 PM    Post subject:

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.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 18 September 2009, 22:51 PM    Post subject:

The thread that Mike referred to contains several different examples. One that I think matches your requirement is shown below.
Code:
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.
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 18 September 2009, 23:51 PM    Post subject:

Thanks, gentlemen. That'll do it.
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 19 September 2009, 1:57 AM    Post subject:

Indeed, the Based method works fine for me. Thanks, again.
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 21 September 2009, 0:27 AM    Post subject:

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:
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?
Back to top
mikep



Joined: 24 Sep 2005
Location: Austin, TX

Posted: 21 September 2009, 1:57 AM    Post subject:

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.
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 21 September 2009, 11:44 AM    Post subject:

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.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 23 September 2009, 20:06 PM    Post subject:

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.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 30 September 2009, 20:06 PM    Post subject:

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.8/ZBasic_2-8-10.zip
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 01 October 2009, 1:42 AM    Post subject:

Excellent. The technique appears to work fine on the ZX-24n now. Thanks, Don.
Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008, 2009, 2010 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group