If I need to fill an array with a single value, is there a typical Forth word to do something like what memset
from C does (that is, set a region of bytes to a specific value)?
1 Answers
In the Forth standard, the word for initializing raw memory is ERASE
, which is in the Core Extensions word set:
ERASE
( addr u -- )
If u is greater than zero, clear all bits in each of u consecutive address units of memory beginning at addr.
ERASE
, as its name suggests, only clears/zeroes memory.
There is also FILL
, in the Core word set, for setting consecutive characters:
FILL
( c-addr u char -- )
If u is greater than zero, store char in each of u consecutive characters of memory beginning at c-addr.
However, the standard does not require characters to be one address unit in size.
If you just want to set a range of address units to a value other than zero, then that is not really a portable or standard compliant operation anyway, so just use FILL
if your address unit is the same size as a character (as it is in most Forths).
The standard also provides BLANK
, in the String word set, for setting a string to spaces:
BLANK
( c-addr u -- )
If u is greater than zero, store the character value for space in u consecutive character positions beginning at c-addr.