PDA

View Full Version : DIM ... AT



ErosOlmi
28-04-2007, 09:02
Some new vaporware but not so much because this functionality is already developed :D
Next thinBasic preview (out by 1st week of May) will have DIM ... AT fucntionality.
Syntax:


DIM VarName[(subscripts)] AS VarType [AT AbsoluteAddress] [ {VALUE | = | VALUE =} InitialValue ]


What is this?
When a varable is created (DIMensioned), thinBasic creates a special control structure that holds variable information plus thinBasic allocates a memory block big enough to hold variable data. This memory block will be than initialized to zero (if variable is numeric) or other pertinent initialization.

If DIM ... AT will be specified, only the special control structure will be create but no data memory block will be allocated. It will be presumed that at the Absolute memory address specified there will be a memory block ready to be used. Also no initialization will take place because some other process or some other variables will be in charge of that data memory block.

An example:

'---Define a standard numeric variable and assign some data
DIM MyLong AS LONG VALUE 1234
'---Now define a new variable of the same type but here set an absolute address
'---This new variable will share the same memory location of the first one
DIM MyNewLong AS LONG AT VARPTR(MyLong)

MSGBOX 0, MyLong '---This will print 1234
MyNewLong = 4321
MSGBOX 0, MyLong '---This will print 4321


Another example:


type MyType
X as single
Y as single
Z as single
end type

dim MyVar as MyType
dim MyNewVar(3) as single at varptr(MyVar)

MyVar.X = 1
MyVar.Y = 2
MyVar.Z = 3

msgbox 0, str$(MyVar.X) & str$(MyVar.Y) & str$(MyVar.Z)

MyNewVar(1) = 10
MyNewVar(2) = 20
MyNewVar(3) = 30

msgbox 0, str$(MyVar.X) & str$(MyVar.Y) & str$(MyVar.Z)


Those are just dummy examples but can give an idea of the power.
The same can be applied to more complex structures, to arrays or to pointers returned by external dll and mapped to script structures.

Just to let you know.
Ciao
Eros

ErosOlmi
28-04-2007, 09:04
Forgot to say, I have also already developed a new function: SetAt
SetAt will let you change, during script execution, the memory address where a previously declared DIM ... AT variable will point to.

ErosOlmi
28-04-2007, 10:11
Better to add also a GetAt function.
It can be used to move current address up/down just few bytes in order to scan memory areas :D

ErosOlmi
28-04-2007, 11:44
OK, GetAt added.
Examples:
addr = GetAt(MyVar) will return the absolute addrees if MyVar is a variable dimensioned with DIM ... AT
SetAt(MyVar, AbsAddress) will set the absolute addrees if MyVar is a variable dimensioned with DIM ... AT

Features will be present in next release.

Petr Schreiber
28-04-2007, 18:22
Hi,

I think this will be very nice celebration of 1st May :D


Bye,
Petr

kryton9
29-04-2007, 04:14
That is a good comment Petr, I didn't know what to say as I don't really understand all the power of this new feature, but I am sure it is if Eros says so.

Petr Schreiber
29-04-2007, 09:33
kryton,

this is very powerful feature for various tricks as well as serious work.
Imagine you want to sync contens of two arrays, or at least part of them.
If you overlap them in memory this way, changing cells from either array A or B will have effect on both.

I am sure Eros will explain us another ways to use


Bye,
Petr

kryton9
29-04-2007, 11:43
That would be a great example, I don't see how you can put 2 different arrays in one area without overwriting one. That is why an example when it is ready will make it easier, but YES that would be something very powerful indeed!!

ErosOlmi
29-04-2007, 19:10
I will give more info when this version will be released becase I'm still working on it.
In few words, a DIM ... AT variable, whatever type, is like a logical structure that overlap a memory area defined somewhere else.
It is a surrogate of pointers that we will try to develop in next months.

Sorry gui, Im out for 2 days. I will give some examples when back.

RobertoBianchi
30-04-2007, 08:37
Eros,

mmh GetAT(), why VARPTR() isn't enough?

Ciao,
Roberto

Petr Schreiber
30-04-2007, 08:52
Hi Roberto,

I think it will keep syntax "clean",
when we have Set everyobdy expects Get :)
Internally it may reference to same sub/function with VARPTR, working just like alias.


Bye,
Petr

RobertoBianchi
30-04-2007, 09:16
Hi Petr,

I understand.
Generally it is always better to avoid than to have same functionality with different name unless these names are fully used in other languages (as an example Console_Print()/Console_Write() and STRPTR ()/VARPTR () functions).
In any case DIM ... AT it's a new powerful instruction and great step to facilitate and make easy the use of pointers!

Ciao,
Roberto

ErosOlmi
30-04-2007, 18:42
Hi all,

yes GetAT and VARPTR seems the same but they are not.
GetAT will return a pointer only if variable has been defined using DIM ... AT.
Another difference is that GetAT accept only variable names returning always the very first byte of the allocation memory while VARPTR accepts also elements of arrays returning the exact position where the element starts in memory.

There are other little differences but marginal.

I'm working also on REDIM ... AT, just for the record :D

Ciao
Eros