PRIVATE Arrays, etc.

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

PRIVATE Arrays, etc.

Post by MattC »

Hi.

In the help it states:
An entire array may be made PRIVATE, following which it is in an undimensioned state. Before the private array can be used within the function or procedure it must be re-dimensioned using a DIM statement. The new dimensions can be the same as or different from those of the original array. PRIVATE arrays are allocated on the heap, but can be accessed only in the function or procedure in which they are declared. [emphasis added]


This seems to imply that the array can/should be re-dimensioned each time with either the same or different dimensions.

However, can any explain why this isn't working?

Code: Select all

      FOR I% = 0 TO 9
        PROCTEST(I%)
      NEXT
      END
      
      DEF PROCTEST(B%)
      PRIVATE C%, A%()
      DIM A%(B%)
      FOR C%=0 TO B%
        PRINT ; A%(C%) ;
      NEXT
      PRINT
      ENDPROC
Or am I misunderstanding the help?

Matt

EDIT: in fact further testing seems to indicate that the array (providing it is the same size) does not have to be re-Dimmed - only the first time. (This was tested using a conditional clause that indicated a return use.)
DDRM

Re: PRIVATE Arrays, etc.

Post by DDRM »

Hi Matt,

The whole point of private variables is that they are preserved between calls to the procedure or function, so it doesn't really make sense to re-dim it each time.

I think what the help is referring to is if you have a private array with the same name as a global array.

The manual DOES imply that you COULD re-DIM it each time - not sure what would happen if you did that, and made it a different size/shape: the data put into it might not be very predictable. Or maybe it would be automatically re-initialised if it gets re-DIMmed.

If you find out, let us know!

Best wishes,

D
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: PRIVATE Arrays, etc.

Post by MattC »

Thanks. What you said makes sense - I just didn't read it that way.

The code that I put here produces a Bad DIM Statement error - that is, when re-dimensioning the array with a different size. Dimensioning the array each time seems to have little further affect other than that of the original dimensioning, and causes no changed to the previous data. If the DIM statement is isolated through a 'first run' conditional clause, the array is still dimensioned on the subsequent runs, so my guess is that any PRIVATE array dimensioning statement is only initiated first time; subsequent dimensioning seems to be ignored.

Thanks again.

Matt