How to get an array size

Blitz3D Forums/Blitz3D Programming/How to get an array size

splinux(Posted 2006) [#1]
Do you know how i can get an array size? I tried to make some routine, but as soon as i reach the last value in the array the program stops saying `array index out of bounds`..


big10p(Posted 2006) [#2]
The only way to do this is to store the array size in a variable, when you dimension it. Or, you could use a bank, instead. You can then use the BankSize command to get it's size.


splinux(Posted 2006) [#3]
Is faster using arrays or banks, to store custom types collections?


big10p(Posted 2006) [#4]
Theoretically, arrays are faster. Plus, you can't use banks for types, unless you use the Handle/Object commands, which will make things a bit slower again.

However, array/bank access speed is unlikely to be your main bottleneck. :)


splinux(Posted 2006) [#5]
i use handles.
thanks.


splinux(Posted 2006) [#6]
how can i get the last unused 4 bytes in a bank? with a for-next?


splinux(Posted 2006) [#7]
I used this code. Please tell me if you find an error.

Global b% = CreateBank(16)
PokeInt b, 0, 1111
PokeInt b, 4, 2222
PokeInt b, 8, 3333

Global i% = 0, start%, finish%, bytes%
.start
If PeekInt(b, i) <> 0
	i = i + 4
	Goto start
Else
	bytes = i/4
	Goto finish
EndIf
.finish

Print bytes
WaitKey()
End


It gives me the first unused int.


big10p(Posted 2006) [#8]
You mean something like this?
free_pos = -1

For i = 0 To (BankSize(bank)-1) Step 4
    If PeekInt(bank,i) = 0
        free_pos = i
        Exit
    EndIf
Next

If free_pos = -1
    ; Bank has no free position. Handle that here.
EndIf



splinux(Posted 2006) [#9]
exactly.


octothorpe(Posted 2006) [#10]
Look into my Container Classes for an alternative - my Vector Container in specific emulates an Array of Handles to objects using a bank. It keeps track of the number of elements in the "array" and can preallocate space for more separately. Maybe you don't have to reinvent the wheel! ;)