Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Passing multi-dimensioned array pointer

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
GTBecker



Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral

Posted: 18 September 2009, 19:31 PM    Post subject: Passing multi-dimensioned array pointer Reply with quote

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
Posts: 765
Location: Austin, TX

Posted: 18 September 2009, 22:32 PM    Post subject: Reply with quote

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
Posts: 2493
Location: Portland, OR

Posted: 18 September 2009, 22:51 PM    Post subject: Reply with quote

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
Posts: 457
Location: Cape Coral

Posted: 18 September 2009, 23:51 PM    Post subject: Reply with quote

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



Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral

Posted: 19 September 2009, 1:57 AM    Post subject: Reply with quote

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



Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral

Posted: 21 September 2009, 0:27 AM    Post subject: Reply with quote

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
Posts: 765
Location: Austin, TX

Posted: 21 September 2009, 1:57 AM    Post subject: Reply with quote

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
Posts: 457
Location: Cape Coral

Posted: 21 September 2009, 11:44 AM    Post subject: Reply with quote

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
Posts: 2493
Location: Portland, OR

Posted: 23 September 2009, 20:06 PM    Post subject: Reply with quote

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
Posts: 2493
Location: Portland, OR

Posted: 30 September 2009, 20:06 PM    Post subject: Reply with quote

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
Posts: 457
Location: Cape Coral

Posted: 01 October 2009, 1:42 AM    Post subject: Reply with quote

Excellent. The technique appears to work fine on the ZX-24n now. Thanks, Don.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 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