Pointer in Basic
#15
Mem is generally the easiest way to go with pointers in QB64.

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.
Reply


Messages In This Thread
Pointer in Basic - by Kernelpanic - 07-16-2023, 09:57 PM
RE: Pointer in Basic - by bplus - 07-16-2023, 10:17 PM
RE: Pointer in Basic - by mnrvovrfc - 07-17-2023, 12:05 AM
RE: Pointer in Basic - by SagaraS - 07-17-2023, 12:17 AM
RE: Pointer in Basic - by OldMoses - 07-17-2023, 12:40 AM
RE: Pointer in Basic - by bplus - 07-17-2023, 01:00 AM
RE: Pointer in Basic - by a740g - 07-17-2023, 02:53 AM
RE: Pointer in Basic - by SpriggsySpriggs - 07-17-2023, 11:55 AM
RE: Pointer in Basic - by Kernelpanic - 07-17-2023, 07:04 PM
RE: Pointer in Basic - by SagaraS - 07-17-2023, 09:39 PM
RE: Pointer in Basic - by mnrvovrfc - 07-17-2023, 10:52 PM
RE: Pointer in Basic - by Kernelpanic - 07-18-2023, 12:17 AM
RE: Pointer in Basic - by SagaraS - 07-18-2023, 02:42 AM
RE: Pointer in Basic - by bplus - 07-18-2023, 12:39 PM
RE: Pointer in Basic - by SMcNeill - 07-18-2023, 04:13 PM
RE: Pointer in Basic - by Kernelpanic - 07-18-2023, 04:26 PM
RE: Pointer in Basic - by SMcNeill - 07-18-2023, 04:59 PM
RE: Pointer in Basic - by SagaraS - 07-18-2023, 04:49 PM
RE: Pointer in Basic - by SMcNeill - 07-18-2023, 05:04 PM
RE: Pointer in Basic - by mnrvovrfc - 07-18-2023, 08:15 PM
RE: Pointer in Basic - by SagaraS - 07-18-2023, 07:30 PM
RE: Pointer in Basic - by SMcNeill - 07-18-2023, 10:11 PM
RE: Pointer in Basic - by SagaraS - 07-19-2023, 10:27 AM
RE: Pointer in Basic - by SMcNeill - 07-19-2023, 01:42 PM
RE: Pointer in Basic - by DSMan195276 - 07-19-2023, 11:15 PM



Users browsing this thread: 5 Guest(s)