Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用Gforth在多个字符串连接上进行堆栈下溢_String_Algorithm_Forth_Gforth_Stackunderflow - Fatal编程技术网

String 使用Gforth在多个字符串连接上进行堆栈下溢

String 使用Gforth在多个字符串连接上进行堆栈下溢,string,algorithm,forth,gforth,stackunderflow,String,Algorithm,Forth,Gforth,Stackunderflow,从中,我使用以下方法将字符串连接到Forth中 s" hello" pad place pad count type s" there!" pad +place pad count type 使用这段代码,我希望能够将多个字符串连接在一起。但是,以下内容在Gforth上失败 s" hi " pad place s" hello " pad place s" world" pad +place pad +place pad count type 从我的基本第四次公开中,我将代码视为将三个

从中,我使用以下方法将字符串连接到Forth中

s" hello" pad place
pad count type
s"  there!" pad +place
pad count type
使用这段代码,我希望能够将多个字符串连接在一起。但是,以下内容在Gforth上失败

s" hi " pad place 
s" hello " pad place
s" world" pad 
+place
pad +place
pad count type
从我的基本第四次公开中,我将代码视为将三个字符串放在堆栈上,然后将字符串附加到堆栈顶部及其下方,然后将新字符串再次附加到堆栈底部


为什么这个代码在最后一个+位置下溢?有办法解决这个问题吗?

您的第二个代码段将“hi”复制到内存位置
pad
,然后将“hello”复制到同一位置(覆盖“hi”)

因此,当您尝试第一个
+位置时
会从堆栈中获取“world”的
addr u
引用,然后将“world”附加到“hello”后面。所以如果你尝试

s" hi " pad place 
s" hello " pad place
s" world" pad +place

//Display the string at pad
pad count type
您应该看到
hello world ok

此时,所有
place
+place
单词都已用完堆栈上的所有字符串引用。如果您刚刚运行,请检查此项

s" hi " pad place 
s" hello " pad place
s" world" pad 
+place

//Check the stack
.s
您将看到一个空堆栈

现在,当您再次使用
pad
时,它会将
pad
表示的地址推送到堆栈中。因此,下一个
+位置
在堆栈上没有字符串引用供其复制,因此它失败

要解决这个问题,你需要像这样的东西

s" hi " pad place 
s" hello " pad +place
s" world" pad +place
pad count type

在这段代码中,“hello”并没有覆盖“hi”,而是附加到它后面,因此下一个
+位置
在堆栈上有正确的参数,并按预期工作

与我的回答无关,
place
似乎没有记录在Gforth手册中,但明确定义了它,这似乎有点奇怪。