Login
Back to forumReply to this topicGo to last reply

Posted By

George
on 2020-02-15
20:36:57
 Redimension Arrays (Basic)

Hi all
Is there any way to redimension an array in Basic 3.5?
Example
10 Dim A(100)
...
20 Dim A(50) <---will raise error

Posted By

gerliczer
on 2020-02-16
02:41:48
 Re: Redimension Arrays (Basic)

No. That's why there is such an error. However, I can imagine that maybe with some heavy trickery you could achieve that. Prerequisites are finding the mechanism to mark a variable for garbage collection, and to trigger the garbage collection. But your example raises the question why would you do exactly what you are doing there? An array of 100 elements can easily accommodate 50.

Posted By

George
on 2020-02-16
08:32:54
 Re: Redimension Arrays (Basic)

It's just an example.
The dimensions change during runtime. Instead if of allocating the most possible length, I want dynamic allocation of arrays.

Maybe has someone has knowledge to share about this topic, how this could be done.

Posted By

SVS
on 2020-02-16
09:48:11
 Re: Redimension Arrays (Basic)

The structure of an array is complex 'cause its ability to manage multiple dimensions. Then reducing the number of elements would not be just a shorting operation, cutting off the exceeding parts.
Of course nothing is impossible on coding, but I suppose an array-reducing routine should be so complex and slow to become unconvenient.
In my programs I'm used to calculate the number of elements before to allocate the array. Not everybody knows that the DIM() accepts variables too, then DIM(in%) can be a flexible way (eventually coupled with a flag-variable to remember if the array is already allocated or not).

Posted By

Csabo
on 2020-02-16
10:00:30
 Re: Redimension Arrays (Basic)

Yeah, just want to chime in and reiterate what's been said; this can't be done in BASIC 3.5.

One workaround would be to issue a CLR in your program, this would let you re-dimension your array, but it would wipe out all other variables as well.

Manually "killing" the array (which is outside of BASIC's capabilities) would be something like this:
0 DIMA$(25)
2 DIMA$(20)
(fails with error)

0 DIMA$(25)
1 POKEPEEK(46)*256+PEEK(45),255
2 DIMA$(20)
This runs, but the memory allocated by the first array is "lost"... And to be honest might have other side effects as well.

Posted By

George
on 2020-02-16
11:00:11
 Re: Redimension Arrays (Basic)

Thank you for the discussion so far...
I come across to this topic, when porting one of my programs to the Atari ST (GFA and Omicron Basic) and Amiga (Amos Basic), where this is possible. Both great platforms by the way.

@SVS: I already do this precalculation and setting the DIM, which is really neat. In my case changing parameters during runtime for a second run, can result into different leghts for 3 different or more arrays, which causes only a problem if you operate at available memory limit.

@Csabo: Your second example is interesting: Would it kill only Array a$? Or every other arrays too?

Another question:
Comparing the runtimes of accessing Arrays (only Integer) from BASIC with direct POKE/PEEK into memory locations, which would run faster? I havent tried yet...
Edit: See my results at the end.

Third question: The BASICS of Atari and Amiga have out of the box very fast (filled)polygon drawing BASIC routines. You pass the (x, y) coords arrays as parameter. I looked in our database, but could not find any existing that can do this on the plus/4. Maybe someone knows...

A small experiment: Comparison Array vs Poke/Peek

10 DIM A%(100)
15 TI$="000000"
20 FOR I=0TO100
30 A%(I)=3
40 X=A%(I)
50 NEXT
55 PRINTTI

56 TI$="000000"
60 FOR I=1TO100
70 POKE(819+I),3
89 X=PEEK(819+I)
90 NEXT
100 PRINTTI

110 TI$="000000"
120 FOR I=819 TO919
130 POKE(I),3
140 X=PEEK(I)
150 NEXT
160 PRINT TI

Results:
60 (Array)
90
48


Posted By

Csabo
on 2020-02-16
15:02:50
 Re: Redimension Arrays (Basic)

My example only "kills" the first allocated variable, which is the array in this case. $2D/$2E points to the location of variables, which is usually right after the last BASIC line.

Posted By

George
on 2020-02-18
06:34:26
 Re: Redimension Arrays (Basic)

I asked about a library for 3d-graphics and after some research i found a real good one from 20 years ago for the C64:

www.ffd2.com/fridge/lib3d/index.html

I has routines for dot, lines and polygons for Hires and MC mode. A very cool basis for 3d-graphics.

It's free for use.

Posted By

MMS
on 2020-02-18
16:39:29
 Re: Redimension Arrays (Basic)

Thank you for sharing, nice find!

I was checking the source code, and supposed it made around the 160x200 multicolor drawing method.
But after checking the pictures I noticed I was wrong.

The fast drawing routines of filled vectors sounds really really promising happy



Back to topReply to this topic


Copyright © Plus/4 World Team, 2001-2024