07-18-2023, 04:13 PM
Mem is generally the easiest way to go with pointers in QB64.
These 2 functions demonstrated here work more-or-less as PEEK and POKE do in QB45, with a few minor exceptions. One, no DEF SEG is required to point to any particular block of memory, and two, you have to specify how many bytes you're reading and writing. 123 as a byte is only 1 byte after all, but 123 as an integer is 2 bytes of memory, and a long is 4 bytes.
Note that in this demo, I use PeekPE and PokePE to point to where two of my variables are in memory -- i and l. If you don't know where you're pointing specifically, then I'd suggest not pointing at anything at all. Just accessing memory at random isn't something I'd recommend to anyone. At least use _MEMNEW to reserve a block of memory for your purposes, rather than pointing all willy-nilly at random areas of mem.
Code: (Select All)
DIM i AS INTEGER, l AS LONG
i = 123
l = 456
PRINT i, PeekPE(_OFFSET(i), 2)
PokePE _OFFSET(i), 126, 2
PRINT i, PeekPE(_OFFSET(i), 2)
PRINT l, PeekPE(_OFFSET(l), 4)
PokePE _OFFSET(l), 456789, 4
PRINT l, PeekPE(_OFFSET(l), 4)
SUB PokePE (o AS _OFFSET, v AS _INTEGER64, b AS _BYTE)
DIM m AS _MEM
m = _MEM(o, b)
SELECT CASE b
CASE 1: _MEMPUT m, m.OFFSET, v AS _BYTE
CASE 2: _MEMPUT m, m.OFFSET, v AS INTEGER
CASE 4: _MEMPUT m, m.OFFSET, v AS LONG
CASE 8: _MEMPUT m, m.OFFSET, v AS _INTEGER64
END SELECT
END SUB
FUNCTION PeekPE (o AS _OFFSET, b AS _BYTE)
DIM m AS _MEM
m = _MEM(o, b)
SELECT CASE b
CASE 1: temp = _MEMGET(m, m.OFFSET, _BYTE)
CASE 2: temp = _MEMGET(m, m.OFFSET, INTEGER)
CASE 4: temp = _MEMGET(m, m.OFFSET, LONG)
CASE 8: temp = _MEMGET(m, m.OFFSET, _INTEGER64)
END SELECT
PeekPE = temp
END FUNCTION
These 2 functions demonstrated here work more-or-less as PEEK and POKE do in QB45, with a few minor exceptions. One, no DEF SEG is required to point to any particular block of memory, and two, you have to specify how many bytes you're reading and writing. 123 as a byte is only 1 byte after all, but 123 as an integer is 2 bytes of memory, and a long is 4 bytes.
Note that in this demo, I use PeekPE and PokePE to point to where two of my variables are in memory -- i and l. If you don't know where you're pointing specifically, then I'd suggest not pointing at anything at all. Just accessing memory at random isn't something I'd recommend to anyone. At least use _MEMNEW to reserve a block of memory for your purposes, rather than pointing all willy-nilly at random areas of mem.