S/SuperBASIC Arrays

Anything QL Software or Programming Related.
User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: S/SuperBASIC Arrays

Post by tofro »

Well, any answer to that depends on the definition of an "array".

Arrays in a classical sense have two constraints to be met: They should hold elements of known, uniform size, tightly packet together in order to not waste memory, be easily and quickly accessible, and, in most compiled languages the programmer should be able to safely assume that there's no "holes" in the allocated memory (that is, for an array of size (n, m), element (2,0) should directly follow element (1,m) in memory. Especially that last constraint makes dynamic arrays extremely expensive to resize - for all changes in size, you have to allocate a new chunk of memory of the new size and move existing elements to that new chunk, then delete the old one. In fact, this chunk allocation tends to keep memory framentation down instead of creating it - If a language starts spreading smaller blocks all over the place, the danger of fragmentation is much bigger.

That chunk allocation & copy is exactly what a dynamic array (which is irritatingly called vector to distinguish it from fixed-size array) in the C++ standard library does, and that's exactly what makes re-sizing a vector expensive there. There's not much you can do to balance between fragmented memory and speed. Other languages than BASIC (like, for example, C++) offer many more data containers than just arrays in order to allow programmers a finer choice between these trade-offs.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
pjw
QL Wafer Drive
Posts: 1286
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: S/SuperBASIC Arrays

Post by pjw »

Tinyfpga wrote:<>
Has anyone created a dynamic array or knows of a better way (in BASIC) of making arrays resizeable without clearing the array.
Its an interesting question! It all depends on what your data needs are, and what constraints you need to impose: Speed, memory, flexibility, complexity,..

I dont know if they do what you want, but IIRC Turbo once advertised something they called "virtual arrays". Basically disk- or medium- based array-like structures. Whether they are resizeable or not, I cant remember. However, it is possible to devise your own that could be.

Another way is to use heap space and expand into that. The simplest way to implement that is to use a real, floating point, array as an index. (While not intended for the purpose you suggest, you may get some idea from a routine I posted on Knoware called UniqueInd) The downside of this method is that its hard to shrink the space again, if you need to. With medium-based virtual arrays that is less of a problem.

If you you are able to use the arrays provided by S*BASIC then there are a number of ways to limit memory consumption and fragmentation. I'll only touch on a few things here: Count your data before dimensioning your array (Typically, eg, for loading a directory's data into an array). If memory is not an object then you can dimension your array to the maximum expected size and limit it to the actual size using a variable index. In fact you can pass such a limited array as a parameter to a procedure and the procedure will act as if the limited array was the whole array. Using LOCal arrays can be quite efficient as the memory used by the array is released again when you exit the procedure. Depending on how your program is structured, this means that the last memory reserved will be released first (LIFO) leaving the memory no worse off than when you entered the routine.

Clearly, there are techniques for creating advanced arrays or array-like structures (like sparse arrays and real "rubber arrays") that may be possible to simulate in S*BASIC or using assembler, but for that you may need to consult your Knuth or read up on later advances on the Internet.

I write this without knowing what you, or anyone else bothering to read this, may already know, in the hope that it may be useful! Happy programming!


Per
dont be happy. worry
- ?
User avatar
mk79
QL Wafer Drive
Posts: 1349
Joined: Sun Feb 02, 2014 10:54 am
Location: Esslingen/Germany
Contact:

Re: S/SuperBASIC Arrays

Post by mk79 »

Tinyfpga wrote:In trying to find the program "sysmon" in Dilwyn's website so that I can test the effect of resizing large arrays
on memory I came across some documentation on Things.
It's part of QPac1: http://www.dilwyn.me.uk/pe/index.html#QPAC1_and_QPAC2


Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

Re: S/SuperBASIC Arrays

Post by Tinyfpga »

Thank you mk79, pjw, tofro and janbredenbeek for your very useful replies.

To better understand arrays I have downloaded sysmon, btnclock (configured to show free memory; what ever that is), memorymap and mem_exe only to find they either do not display in a window or they are faulty. In the process of
learning how to program in HD I have also noticed a weakness in the display of loose buttons

I know this is a QL forum and not an SMSQE forum and it is likely that my use of SMSQE at high resolutions is a minority interest but my problems with memory and buttons may be of interest to others, so should I post them here, keep quiet or start a new topic.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: S/SuperBASIC Arrays

Post by tofro »

Tinyfpga wrote: I know this is a QL forum and not an SMSQE forum ...
In that case, you know something we don't. ;)

Anthing SBASIC/SuperBASIC related is on-topic here, go ahead.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
mk79
QL Wafer Drive
Posts: 1349
Joined: Sun Feb 02, 2014 10:54 am
Location: Esslingen/Germany
Contact:

Re: S/SuperBASIC Arrays

Post by mk79 »

Tinyfpga wrote:I know this is a QL forum and not an SMSQE forum and it is likely that my use of SMSQE at high resolutions is a minority interest but my problems with memory and buttons may be of interest to others, so should I post them here, keep quiet or start a new topic.
SMSQ/E is a QL operating system, why shouldn't it be on topic? But I think you should probably start a new thread with any button related issues.


Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

Re: S/SuperBASIC Arrays

Post by Tinyfpga »

The reason for my hesitancy stems from my interest in both SMS2 and Stella neither of which are QL operating systems
but were both written by Tony Tebby.

Sadly they were rejected by QL users and subsequently TT gave up developing operating systems. I see some of his
ideas living on in the SMS2 variant SMSQE, and it is this OS that I enjoy using on various platforms, thanks to the
hard work of a dedicated group of enthusiasts.

I don't have a QL, I ignore the Sbasic interpreter and I can't see the point of running SMSQE on a QL (I didn't
realise a vanilla QL supported it) and so I feel I am bit of an interloper on this forum.

Anyway thank you for your encouragement and once I have gathered my thoughts I will start a topic "memory and buttons"
Last edited by Tinyfpga on Wed May 19, 2021 1:20 pm, edited 1 time in total.


User avatar
mk79
QL Wafer Drive
Posts: 1349
Joined: Sun Feb 02, 2014 10:54 am
Location: Esslingen/Germany
Contact:

Re: S/SuperBASIC Arrays

Post by mk79 »

Tinyfpga wrote:I don't have a QL, I ignore the Sbasic interpreter and I can't see the point of running SMSQE on a QL (I didn't
realise a vanilla QL supported it)
Well, it doesn't (yet). Currently you need one of the GoldCards. But QPC and all emulators are on-topic here, too, so I think you're taking the "Sinclair QL" in the forum title a little bit too literally.


User avatar
vanpeebles
Commissario Pebbli
Posts: 2816
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: S/SuperBASIC Arrays

Post by vanpeebles »

It's impossible to pigeon hole everything into their own little categories, we are pretty laid back here :)


Post Reply