PDA

View Full Version : Long-as pointers, is problem?



ReneMiner
29-05-2013, 14:31
Could the use of Long as Pointers - especially stringpointers - be a problem?



Uses "Console"

$Mousewheel = "MouseWheel" : %MouseWheel = StrPtr($MouseWheel)
$MouseMove = "MouseMove" : %MouseMove = StrPtr($MouseMove)

Long myMessage = %MouseWheel

Printl peek$(myMessage, strptrLen(myMessage))

WaitKey

works so far. But could it be a problem if a Pointer exceeds &H7FFFFFFF ? Or will stringpointers never do that?

ErosOlmi
29-05-2013, 22:33
Rene,

you have the ability to find thinBasic keywords that even I do not remember to have developed :)

Where memory is allocated is on the hands of the OS Kernel. The precise location where the memory is allocated is not known in advance.

In LONG variables the first bit is used for the sign but when you store a pointer into a LONG, the sign has no meaning, it is just a number stored into a 4 byte.
When such data is passed to a function expecting a pointer, the first bit is not used as sign and all is interpreted as a DWORD.

So, in thinBasic use LONG and DWORD as interchangeable when storing a pointer.
Important is not to make any calculation using LONG storing pointers otherwise it is better to use DWORD.

Ciao
Eros

D.J.Peters
30-05-2013, 16:02
On 32 Bit OS the addresses of virtual memory (in userspace not OS kernalspace) are never 32 bit.
That means you can ignore the sign bit of LONG (it's never "1")

For pointer calculation i would prefer DWORD as pointer type.

Joshy

ReneMiner
30-05-2013, 16:57
Yes, I would recommend/prefer Dwords too, but the constants are Longs by default. Since I use them absolute and do not calculate with them and because Eros says it's ok, I'll make my "constant string-enumerations" like this now using Longs.
Just for interest: is there a way to make some Dword-constants?

Petr Schreiber
30-05-2013, 17:23
Just for interest: is there a way to make some Dword-constants?

You can do it like this:


Const MyConstant As DWord = 5


...or like this:


%myConstant = 5 AS DWORD



Petr

ReneMiner
30-05-2013, 17:27
Great- that means I can play on the safe side now :D