QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _MEMCOPY statement copies a block of bytes from one memory offset to another offset in memory.

Syntax

_MEMCOPY sourceBlock, sourceBlock.OFFSET, sourceBlock.SIZE TO destBlock, destBlock.OFFSET

Parameter(s)

Description

Example(s)

Swapping data from one STRING variable to another. Fixed length strings are recommended for speed.


DIM m AS _MEM
DIM n AS _MEM

m = _MEMNEW(10)
n = _MEMNEW(100)

_MEMPUT m, m.OFFSET, "1234567890"

s$ = SPACE$(10) 'to load into a variable length string set its length first
_MEMGET m, m.OFFSET, s$
PRINT "in:[" + s$ + "]"

_MEMCOPY m, m.OFFSET, m.SIZE TO n, n.OFFSET 'put m into n

b$ = SPACE$(10)
_MEMGET n, n.OFFSET, b$
PRINT "out:[" + b$ + "]" 
_MEMFREE m: _MEMFREE n 'always clear the memory when done 

Snippet: Instead of copying each array element, one at a time in nested FOR…NEXT loops, _MEMCOPY does it in one statement instantly.


'copy array a to array b one index at a time:
FOR i1 = 0 TO 100
    FOR i2 = 0 TO 100
        b(i1, i2) = a(i1, i2)
    NEXT
NEXT

'copy array a to array b in memory instantly:
DIM ma AS _MEM: ma = _MEM(a()) 'place array data into blocks
DIM mb AS _MEM: mb = _MEM(b())
_MEMCOPY ma, ma.OFFSET, ma.SIZE TO mb, mb.OFFSET
_MEMFREE ma: _MEMFREE mb 'clear the memory when done 

See Also