QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Qwerkey on January 29, 2019, 06:52:50 am

Title: DIM SHARED Array Does Not Transfer Into/Out Of Subroutine Correctly
Post by: Qwerkey on January 29, 2019, 06:52:50 am
In my latest project, I ran into problems with an array not updating properly when put through a subroutine: all the array elements returned zeroes when they should have had non-zero values.

In the code here, Positions!() is a DIM SHARED Array which in the subroutine SetData has its elements set.

If you run the code as is, you will see that the displayed elements have non-zero values.

If you then comment out lines 44 to 53 and uncomment lines 39 to 42, then all the array elements end up zero.  In lines 39 to 42, the subroutine Angle is called: this subroutine performs the same operations as in lines 44 to 53.

It seems that a DIM SHARED Array does not like to be put through a subroutine (is this an expected property of a shared array?).  The subroutine works properly (of course) with stand-alone variables and with non-shared arrays.

I don't believe that I'm doing something stupid here (I've checked it out thrice), but that is a possibility.
Code: QB64: [Select]
  1. CONST Pi! = 3.141592, Hours! = -Pi! / 6, ZOffset% = -398, XImage% = 182, YImage% = 252
  2. CONST Halfwidth%% = 50, Halfheight%% = Halfwidth%% * YImage% / XImage%, Radius% = 320, Tucked%% = 7
  3. DIM SHARED Positions!(4, 12, 1, 4), Phi!(12)
  4.  
  5. CALL SetData
  6.  
  7. FOR N%% = 0 TO 11
  8.     FOR S%% = 0 TO 3
  9.         PRINT N%%, S%%, Positions!(S%%, N%%, 0, 0); Positions!(S%%, N%%, 1, 0)
  10.     NEXT S%%
  11. NEXT N%%
  12.  
  13.  
  14. SUB SetData
  15.     FOR N%% = 0 TO 6
  16.         Phi!(N%%) = N%% * Hours!
  17.     NEXT N%%
  18.     FOR N%% = 7 TO 11
  19.         Phi!(N%%) = (N%% - 12) * Hours!
  20.     NEXT N%%
  21.     FOR S%% = 0 TO 4
  22.         Positions!(S%%, 0, 0, 4) = 0
  23.         Positions!(S%%, 0, 1, 4) = Radius% - Tucked%% * S%%
  24.         Positions!(S%%, 0, 0, 3) = Positions!(S%%, 0, 0, 4) + Halfwidth%%
  25.         Positions!(S%%, 0, 1, 3) = Positions!(S%%, 0, 1, 4) - Halfheight%%
  26.         Positions!(S%%, 0, 0, 2) = Positions!(S%%, 0, 0, 4) - Halfwidth%%
  27.         Positions!(S%%, 0, 1, 2) = Positions!(S%%, 0, 1, 4) - Halfheight%%
  28.         Positions!(S%%, 0, 0, 1) = Positions!(S%%, 0, 0, 4) + Halfwidth%%
  29.         Positions!(S%%, 0, 1, 1) = Positions!(S%%, 0, 1, 4) + Halfheight%%
  30.         Positions!(S%%, 0, 0, 0) = Positions!(S%%, 0, 0, 4) - Halfwidth%%
  31.         Positions!(S%%, 0, 1, 0) = Positions!(S%%, 0, 1, 4) + Halfheight%%
  32.     NEXT S%%
  33.     FOR S%% = 0 TO 4
  34.         FOR N%% = 1 TO 11
  35.  
  36.             'CALL Angle doesn't work with Positions!()
  37.             'CALL Angle(Positions!(S%%, 0, 0, 0), Positions!(S%%, 0, 1, 0), Positions!(S%%, N%%, 0, 0), Positions!(S%%, N%%, 1, 0), Phi!(N%%))
  38.             'CALL Angle(Positions!(S%%, 0, 0, 1), Positions!(S%%, 0, 1, 1), Positions!(S%%, N%%, 0, 1), Positions!(S%%, N%%, 1, 1), Phi!(N%%))
  39.             'CALL Angle(Positions!(S%%, 0, 0, 2), Positions!(S%%, 0, 1, 2), Positions!(S%%, N%%, 0, 2), Positions!(S%%, N%%, 1, 2), Phi!(N%%))
  40.             'CALL Angle(Positions!(S%%, 0, 0, 3), Positions!(S%%, 0, 1, 3), Positions!(S%%, N%%, 0, 3), Positions!(S%%, N%%, 1, 3), Phi!(N%%))
  41.  
  42.             Positions!(S%%, N%%, 0, 0) = CINT(Positions!(S%%, 0, 0, 0) * COS(Phi!(N%%)) - Positions!(S%%, 0, 1, 0) * SIN(Phi!(N%%)))
  43.             Positions!(S%%, N%%, 1, 0) = CINT(Positions!(S%%, 0, 0, 0) * SIN(Phi!(N%%)) + Positions!(S%%, 0, 1, 0) * COS(Phi!(N%%)))
  44.             Positions!(S%%, N%%, 0, 1) = CINT(Positions!(S%%, 0, 0, 1) * COS(Phi!(N%%)) - Positions!(S%%, 0, 1, 1) * SIN(Phi!(N%%)))
  45.             Positions!(S%%, N%%, 1, 1) = CINT(Positions!(S%%, 0, 0, 1) * SIN(Phi!(N%%)) + Positions!(S%%, 0, 1, 1) * COS(Phi!(N%%)))
  46.             Positions!(S%%, N%%, 0, 2) = CINT(Positions!(S%%, 0, 0, 2) * COS(Phi!(N%%)) - Positions!(S%%, 0, 1, 2) * SIN(Phi!(N%%)))
  47.             Positions!(S%%, N%%, 1, 2) = CINT(Positions!(S%%, 0, 0, 2) * SIN(Phi!(N%%)) + Positions!(S%%, 0, 1, 2) * COS(Phi!(N%%)))
  48.             Positions!(S%%, N%%, 0, 3) = CINT(Positions!(S%%, 0, 0, 3) * COS(Phi!(N%%)) - Positions!(S%%, 0, 1, 3) * SIN(Phi!(N%%)))
  49.             Positions!(S%%, N%%, 1, 3) = CINT(Positions!(S%%, 0, 0, 3) * SIN(Phi!(N%%)) + Positions!(S%%, 0, 1, 3) * COS(Phi!(N%%)))
  50.             Positions!(S%%, N%%, 0, 4) = CINT(Positions!(S%%, 0, 0, 4) * COS(Phi!(N%%)) - Positions!(S%%, 0, 1, 4) * SIN(Phi!(N%%)))
  51.             Positions!(S%%, N%%, 1, 4) = CINT(Positions!(S%%, 0, 0, 4) * SIN(Phi!(N%%)) + Positions!(S%%, 0, 1, 4) * COS(Phi!(N%%)))
  52.  
  53.         NEXT N%%
  54.     NEXT S%%
  55.     FOR S%% = 0 TO 4
  56.         Positions!(S%%, 12, 0, 0) = Tucked%% * (3 - S%%) - Halfwidth%%
  57.         Positions!(S%%, 12, 1, 0) = Halfheight%%
  58.         Positions!(S%%, 12, 0, 1) = Positions!(S%%, 12, 0, 0) + 2 * Halfwidth%%
  59.         Positions!(S%%, 12, 1, 1) = Halfheight%%
  60.         Positions!(S%%, 12, 0, 2) = Tucked%% * (3 - S%%) - Halfwidth%%
  61.         Positions!(S%%, 12, 1, 2) = -Halfheight%%
  62.         Positions!(S%%, 12, 0, 3) = Positions!(S%%, 12, 0, 2) + 2 * Halfwidth%%
  63.         Positions!(S%%, 12, 1, 3) = -Halfheight%%
  64.         Positions!(S%%, 12, 0, 4) = Tucked%% * (3 - S%%)
  65.         Positions!(S%%, 12, 1, 4) = 0
  66.     NEXT S%%
  67.  
  68. SUB Angle (Xin%, Yin%, Xout%, Yout%, Theta!)
  69.     Xout% = CINT(Xin% * COS(Theta!) - Yin% * SIN(Theta!))
  70.     Yout% = CINT(Xin% * SIN(Theta!) + Yin% * COS(Theta!))
  71.  
  72.  
Title: Re: DIM SHARED Array Does Not Transfer Into/Out Of Subroutine Correctly
Post by: FellippeHeitor on January 29, 2019, 06:59:35 am
Angle() expects integers and your array is of type single. Values enter the SUB but can’t be altered if types don’t match.
Title: Re: DIM SHARED Array Does Not Transfer Into/Out Of Subroutine Correctly
Post by: Qwerkey on January 29, 2019, 07:24:11 am
Ah, I thought that integers and singles could swap freely (the single gets "integerised" in the process).  So it was incompetence, after all.
Title: Re: DIM SHARED Array Does Not Transfer Into/Out Of Subroutine Correctly
Post by: Pete on January 29, 2019, 10:31:21 am
How did you miss it on try 3? That was supposed to be the "charm."

I'm just happy a shared array can be re-dimensioned in a sub when I need that done. What I've been caught on a couple of times is incorrect variable results because I'll declare a variable as an integer and expand my project to the point some functions surpass the limits of the integer declaration.

Well I see lots of numbers but even with the angle call corrected, I have no idea what this data will be used for. I'll guess a clock. Am I close?

Pete
Title: Re: DIM SHARED Array Does Not Transfer Into/Out Of Subroutine Correctly
Post by: Qwerkey on January 29, 2019, 10:59:38 am

Well I see lots of numbers but even with the angle call corrected, I have no idea what this data will be used for. I'll guess a clock. Am I close?

Pete

Pete, the code I gave was just a demo of the problem I had.  In fact the original difficulty occurred in my Clock Patience program, so yes you were close.  I'm looking forward to that day when I actually know what I'm doing with this marvellous resource which is QB64.