PDA

View Full Version : Data type of pointer



Benjamin
20-12-2023, 03:20
If I want to do a PEEK of a memory address that I got from VARPTR, then I need to know the data type of a pointer, right? So what is the data type of a pointer? I mean, obviously it's a numeric data type, and it's a memory address, so it can't be BYTE.

Benjamin
20-12-2023, 04:17
I double checked the manual on PEEK, and saw that specifying the type was optional. This is the little experiment I was fooling with. I tried calling VARPT with PEEK and that gave me the value at the variable.


USES "CONSOLE"
REM First, we declare variables a, b, and c
DIM a AS BYTE
DIM b AS BYTE
DIM c AS BYTE
REM Second, we assign a value to each variable
a = 1
b = 2
c = 3
REM Third, we print the address of each variable
PRINTL VARPTR(a)
PRINTL VARPTR(b)
PRINTL VARPTR(c)


So, I tried doing "PRINTL PEEK(VARPTR(a))" and that gave me the value at the variable, so I know that if I have the value stored at the pointer, I can use PEEK to get the value stored in the variable. I'm sure this is patently obvious to anyone that knows what they're doing.

ErosOlmi
24-12-2023, 13:02
A pointer is a number that points to a memory area.

thinBasic is a 32bit only process so under 32bit process, a pointer is 32bit so usually a DWORD but also a LONG is OK (internally the sign bit will not be interpreted as sign but will be part of the addressing)

Under a 64bit process, memory addressing pointer is represented by a QUAD (64 bit number)

What is allocated into a memory area and how it is interpreted is responsibility of the process and programmer.
The Operating system is just responsible of allocating/deallocating but how that memory are is interpreted is not a OS job.
The same memory area con be interpreted ad a number or a string or a type or whatever.

Just an example


USES "CONSOLE"
REM First, we declare variables a, b, and c
DIM a AS BYTE
DIM b AS BYTE
DIM c AS BYTE
REM Second, we assign a value to each variable
a = 1
b = 2
c = 3
REM Third, we print the address of each variable
printl "Defined 3 byte var: a, b, c, d"
printl "Values of a, b, c...........:", a,b,c
PRINTL "Varptr of a, b, c...........:", VARPTR(a), VARPTR(b), VARPTR(c)
PRINTL "Peek(Varptr) of a, b, c.....:", peek(byte, VARPTR(a)), peek(byte, VARPTR(b)), peek(byte, VARPTR(c))
printl "-----------------------------"


dim nLong as Long
nlong = 1234567898
printl "Defined 1 long var: nLong"
printl "Values nLong...............:", nLong
PRINTL "Varptr of nLong............:", VARPTR(nLong)
PRINTL "Peek(Varptr) of nLong......:", peek(long, VARPTR(nLong))
printl "-----------------------------"


dim bArray(4) as byte at varptr(nLong)
printl "Defined 1 array of 4 bytes at the memory location of nLong so the memory holding nLong can be interpreted in a different way"
printl "Values bArray(1)...............:", bArray(1)
printl "Values bArray(2)...............:", bArray(2)
printl "Values bArray(3)...............:", bArray(3)
printl "Values bArray(4)...............:", bArray(4)
PRINTL "Varptr bArray..................:", VARPTR(bArray(1)), "Should be the same as VARPTR(nLong) above"


WaitKey