PDA

View Full Version : Pointers and dereferenciation



ErosOlmi
25-04-2007, 10:32
Another thing we are working on is full support of variable pointers and pointer dereferenciation.
Dereferencing is a powerful way to access data using pointers. Many 3rd party library returns pointers to internal structures to access them. Right now the only way to get/set real data values from a pointer is using PEEK/POKE knowing the exact position of a structure element and element type. If we will success it will be easy to use pointers both for standard scripts or for script accessing external data.

But what is dedereferenciation? A little example, I will be more precise in further posts.
Imagine you have a dynamic string.


DIM MyString as string

now you have a pointer to that string


dim MyStrPtr as long
MyStrPtr = strptr(MyString)

The only way to acces the string using that generic pointer will be something like:


Dim aBuffer as string
aBuffer = peek$(MyStrPtr, len(MyString))


Using a pointer it will be something like:


dim MyString as string
dim MyStrPtr as string PTR = varptr(MyString)

Acessing the string data will be something like:


dim aBuffer as string = @MyStrPtr


I will add further info on them. We are trying to solve some internal programming strategy we need to change from current one in order to add those functionalities.

As a reference, please see Power Basic explanation of what is a pointer at http://www.powerbasic.com/support/help/pbwin/html/Pointers_(@).htm
We are trying to follow those indications.

Regards
eros

Petr Schreiber
25-04-2007, 13:14
Eros,

another nice surprise !
The @ syntax is my favorite :)

Bye,
Petr

ErosOlmi
25-04-2007, 19:10
After finishing nested UDT I will work on this.
I too like using pointers.

kryton9
25-04-2007, 20:56
From the little I understand, classes are udt's with pointers to the functions. Does this mean classes are in the making in the future?

ErosOlmi
25-04-2007, 22:00
No. We are talking about standard pointers to basic data (numeric or string) or to UDT.
No OOP here ;D

Almost all is pointer in programing. The programming language hide this complexity.

kryton9
26-04-2007, 02:40
What I don't understand is the benefit over just using a global variable? It seems like the same thing when you look at it.

ErosOlmi
26-04-2007, 12:21
Global variables are ok for many aspects but globals can create big troublers for code maintainability.
When code become a bit more complex, in case of error it will be difficult to debug.
Another problem using globals is when you divide you code into multiple include files. In theory every include file should be organized in such a way it can re-used by other applications. But is you use global variables it will be a nightmare.

Also global will not let you create re-entrant multi-thread code in DLL because structure is just one.
In those cases the way is to allocate memory by hand and returna pointer to allocated memory structures. Every thread will have its own pointer to its own structure.

In any case I'm talking about not simple source, I mean sources of al least 4/5 thousands lines. thinCore is now more than 145000 line sof code so you can imagine.

I hope to be able to have pointers reasy as soon as possible so we will be able to so some of the advantages.
A real examp,e can be see into lates ODE example from Petr here: http://community.thinbasic.com/index.php?topic=805.msg4980#msg4980
If you look at the sources you will see that ODE lib returns a pointer to a structure and Petr was oblidge to use POKE/PEEK to access structure data. With a pointer to a logic structure this will not be necessary and a more simple syntax will take place. Also there will be no need to move piece of memory to access data members.

Eros

kryton9
26-04-2007, 12:46
Thanks, I have some good reading when I have time with those links to study. Thanks for the explanation too.