我正在尝试在 gforth 中附加两个字符串,但我收到了一些看起来很吓人的错误消息。
虽然s" foo" s" bar" append type cr
工作正常,但一旦我开始将字符串存储在变量中或从单词中创建它们,就会出现错误。例如:
: make-string ( -- s )
s" foo" ;
: append-print ( s s -- )
append type cr ;
make-string s" bar" append-print
运行它会产生以下错误:
$ gforth prob1.fs -e bye
gforth(41572,0x7fff79cc2310) malloc: *** error for object 0x103a551a0: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6.
我精通C语言,所以很明显我错误地使用了Forth !我想我需要学习一些关于 Forth 内存管理的基本知识。
谁能解释一下这里出了什么问题,我应该怎么做?
当我尝试附加存储在变量中的字符串时,我也遇到了问题:
variable foo
s" foo" foo !
foo s" bar " append type cr
这以我必须打破的循环结束:
$ gforth prob2.fs
foo��^C
in file included from *OS command line*:-1
prob2.fs:4: User interrupt
foo s" bar " append >>>type<<< cr
Backtrace:
$10C7C2E90 write-file
作为参考,我在 Mac OS X 上使用 gforth 0.7.2。我将非常感谢您对正在发生的事情的一些很好的解释。
更新
我可以看到的定义append
:
see append
: append
>l >l >l >l @local0 @local1 @local3 + dup >l resize throw >l @local4 @local0 @local3 + @local5
move @local0 @local1 lp+!# 48 ; ok
那么,似乎我需要自己在 Forth 中管理内存?如果是这样,怎么做?
解决方案
Andreas Bombe 提供了以下线索。最终可行的程序是
: make-string ( -- s )
s" foo" ;
: append-print
s+ type cr ;
make-string s" bar" append-print
输出是
$ gforth b.fs -e bye
foobar