append
uses resize
on the first string make space to append the second string. This requires that the string be allocated on the heap.
When you compile a string with s"
into a word, it gets allocated in the dictionary. If you try resize
(directly or indirectly through append
) on that pointer you will get the error you see.
Normally s"
has undefined interpretation semantics. Gforth defines its interpretation semantics for convenience as allocating the string on the heap. That's why it works (in gforth) as long as you don't compile it.
Edit:
I've found the definition of append
, it's part of libcc.fs
(a foreign function interface builder as it seems) and not a standard word. This is the definition in the source, more readable than the see
decompile:
: append { addr1 u1 addr2 u2 -- addr u }
addr1 u1 u2 + dup { u } resize throw { addr }
addr2 addr u1 + u2 move
addr u ;
Immediately before that is a definition of s+
:
: s+ { addr1 u1 addr2 u2 -- addr u }
u1 u2 + allocate throw { addr }
addr1 addr u1 move
addr2 addr u1 + u2 move
addr u1 u2 +
;
As you can see this one allocates new memory space instead of resizing the first string and concatenates both strings into it. You could use this one instead. It is not a standard word however and just happens to be in your environment as an internal implementation detail of libcc.fs
in gforth so you can't rely on it being available elsewhere.