MemCpy

Purpose

Copies a block of memory in fastest possible way.

Syntax

MemCpy dst, src, cnt

MemCpy(dst, src, cnt)

Description

The first parameter of MemCpy is the address of the destination and the second one the one of the source and the third one can be a constant or, for example, the length of the source to copy.

MemCpy is extremely efficient in copying Type variables. MemCpy is one of the rare commands that is compiled inline when cnt is a constant (not a function).

Example

Local a$ = "GFA Basic", b$ = Space(9)

MemCpy V:b$, V:a$, 9            // This works as described

Print a$, b$

a$ = "GFA Basic", b$ = Space(9)

MemCpy V:b$, V:a$, Len(b$)      // This doesn't work this way...

Print a$, b$

a$ = "GFA Basic", b$ = Space(9)

MemCpy V:a$, V:b$, Len(b$)      // ..but for some reason, does this way

Print a$, b$

a$ = "GFA Basic", b$ = Space(9)

MemCpy V:b$, V:a$, 9            // Once again, this one works fine

Print a$, b$

Known Issues

As can be seen in the example above, if cnt is not a constanrt, the command does not work correctly: in fact, if you don't use a constant, then the dst and src values are reversed and memory written from destination to source.

This is a bug with the compiler and, as such, will not be fixed in the near future. Instead, use MemMove, BMove or BlockMove which may be slower as they check for overlaps but work as expected; either that, or always use non-constants and remember to reverse the destination and source addresses.

Remarks

MemCpy is highly compatible to the C function memcpy(). If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use MemMove, Bmove, or BlockMove to handle overlapping regions.

The code for MemCpy is extremely small and fast as it uses a single assembly command to carry out the transfer.

See Also

BMove, BlockMove, MemMove

{Created by Sjouke Hamstra; Last updated: 28/03/2023 by James Gaite}