View Full Version : Array Bounds
Hi Eros,
Array bounds are a handicap, is there a possibility to switch off?
Normally ignores the Interpreter/Compiler this and goes ahead with the next condition?
I had have never a problem with this array: 'Map(x,y-1)' y gets zero and this activates
the array bounds. That's no good! :evil:
Petr Schreiber
04-11-2012, 16:39
ThinBASIC arrays are 1 based, so generating error when trying to access index of 0 is correct. You need to arrange your code to use only the valid indexes.
Once your code has no out of bounds errors, you can actually disable bound checks, if you want, to get speed improvement. This is done by:
#DEFAULT BOUNDCHECK OFF
Petr
Thanks Petr. It is important for my game.
One more question: how get I memory, in order to work with poke and peek ?
ErosOlmi
04-11-2012, 17:55
You can allocate dinamic memory using:
dynamic string, than use STRPTR to have memory address
any array of base data (not dynamic strings) and than use VARPTR to have memory address
thinBasic Heap.... functions http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?heap_alloc.htm
... other ways ...
In all cases your script is limited to 2Gb of total memory :D
'-Using string------------------------------------------------------------------
Dim MyString As String
Dim pMemS As Long
'---Create storage
MyString = String$(10, $NUL)
'---Get pointer to first byte of string
'---ATTENTION, every time string content is updated using an
' assignment, string memory allocation change because dynamic strings are always de-allocated and allocated
' again when assigned
pMemS = StrPtr(MyString)
'---Store 3 bytes
Poke$(pMemS, "AAA")
'---Retrive 3 bytes
MsgBox 0, "Using dynamic string: " & Peek$(pMemS, 3)
'---Free mem
MyString = ""
'Using Heap---------------------------------------------------------------------
'---Alloc some memory
Dim pMem As Long = HEAP_Alloc(10)
'---Store 3 bytes
Poke$(pMem, "AAA")
'---Retrive 3 bytes
MsgBox 0, "Using Heap: " & Peek$(pMem, 3)
'---Free mem
HEAP_Free(pMem)
Once you have your memory pointer, you can poke/peek in whatever position as far as you do not go outside of allocated area by that pointer otherwise GPF is just round the corner.
Thanks Eros. This is easy to do.
ErosOlmi
04-11-2012, 18:57
Do not forget that thinBasic give programmer the freedom to use allocated memory in dynamic ways using virtual memory structures created with DIM ... AT ...
For example, once you have allocated 10 bytes of memory you can use that area as you prefer by direct poke/peek or by creating virtual variables and over imposing them to you memory.
'Using Heap---------------------------------------------------------------------
'---Alloc mem
Dim pMem As Long = HEAP_Alloc(10)
'---Store 4 bytes
Poke$(pMem, "AAAA")
'---Retrive 4 bytes
MsgBox 0, "Using Heap: " & Peek$(pMem, 4)
'---The following two virtual variables shares the same memory
Dim TwoLongs(2) As Long At pMem
Dim TenBytes(10) As Byte At pMem
TwoLongs(1) = 123 '---Here you are filling a long but also filling byte 1 of TenBytes
TwoLongs(2) = 244 '---Here you are filling a long but also filling byte 5 of TenBytes
MsgBox 0, Join$(TenBytes, ",")
'---Free mem
HEAP_Free(pMem)
You can do the above with any kind of variables able to be created with DIM ... AT ...
ThinBasic will not allocate or de-allocate any memory but will use the one passed.
Even if the variable is LOCAL to a function, when variable will be destroyed at function end, if created with DIM ... AT ... variable memory will not be de-allocated.
It is programmer responsibility to free such memory
Ciao
Eros
ReneMiner
04-11-2012, 19:54
'---Store 3 bytes
Poke$(pMem, "AAAA")
'---Retrive 3 bytes
MsgBox 0, "Using Heap: " & Peek$(pMem, 4)
what now ? 3 or 4 ? don't try to confuse me more that I am already :shock:
ErosOlmi
05-11-2012, 08:02
Do not be confused: just copy / paste from previous example, than change number of bytes but forgot to amend the comment.
Hope you get also the meaning and not just the little error in the comment.