Passing multi-dimensioned array pointer

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Passing multi-dimensioned array pointer

Post 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?
Tom
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post 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.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post by GTBecker »

Thanks, gentlemen. That'll do it.
Tom
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post by GTBecker »

Indeed, the Based method works fine for me. Thanks, again.
Tom
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post 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&#40;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&#41;
&#123;
	float zv_NewCovars&#91;4&#93;;
	#define zv_Covars &#40;&#40;float *&#41;&#40;zp_CovarsAddr&#41;&#41;

	*zp_Rate = *zp_RawGyro - *zp_Bias;
	*zp_Angle = *zp_Angle + &#40;*zp_Rate * zp_dT&#41;;
	zv_NewCovars&#91;1 - 1&#93; = &#40;*zp_ProcessNoise - zv_Covars&#91;2 - 1&#93;&#91;1 - 1&#93;&#41; - zv_Covars&#91;1 - 1&#93;&#91;2 - 1&#93;;
	zv_NewCovars&#91;2 - 1&#93; = -zv_Covars&#91;2 - 1&#93;&#91;2 - 1&#93;;
	zv_NewCovars&#91;3 - 1&#93; = -zv_Covars&#91;2 - 1&#93;&#91;2 - 1&#93;;
	zv_NewCovars&#91;4 - 1&#93; = *zp_GyroNoise;
	zv_Covars&#91;1 - 1&#93;&#91;1 - 1&#93; = zv_Covars&#91;1 - 1&#93;&#91;1 - 1&#93; + &#40;zv_NewCovars&#91;1 - 1&#93; * zp_dT&#41;;
	zv_Covars&#91;2 - 1&#93;&#91;1 - 1&#93; = zv_Covars&#91;2 - 1&#93;&#91;1 - 1&#93; + &#40;zv_NewCovars&#91;2 - 1&#93; * zp_dT&#41;;
	zv_Covars&#91;1 - 1&#93;&#91;2 - 1&#93; = zv_Covars&#91;1 - 1&#93;&#91;2 - 1&#93; + &#40;zv_NewCovars&#91;3 - 1&#93; * zp_dT&#41;;
	zv_Covars&#91;2 - 1&#93;&#91;2 - 1&#93; = zv_Covars&#91;2 - 1&#93;&#91;2 - 1&#93; + &#40;zv_NewCovars&#91;4 - 1&#93; * zp_dT&#41;;
	#undef zv_Covars
&#125;
Any hint there?
Tom
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post 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.
Mike Perks
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post 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.
Tom
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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
- Don Kinzer
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Post by GTBecker »

Excellent. The technique appears to work fine on the ZX-24n now. Thanks, Don.
Tom
Post Reply