【问题标题】:AppleScript - StackOverflow errorAppleScript - StackOverflow 错误
【发布时间】:2011-10-26 04:21:39
【问题描述】:

我今天刚开始使用applescript,听说了子程序。所以我决定编写一个小测试程序,它接受一个数字,将其加 9,减 27,除以 3,然后返回结果。只有它不返回结果;它会返回 StackOverFlow 错误。什么是 StackOverFlow 错误?

程序编译正确,不知道哪里出错了。就像我说的,我非常是 applescript 的新手。这是我正在运行的代码:

calculate_result(text returned of (display dialog "Enter a number:" default answer ""))

on calculate_result(this_result)
    set this_result to this_result + 9
    set this_result to this_result - 27
    set this_result to this_result / 3
    return calculate_result(this_result)
end calculate_result

【问题讨论】:

    标签: applescript stack-overflow subroutine


    【解决方案1】:
    return calculate_result(this_result)
    

    您再次递归调用子例程,将this_result 传递给它,被调用的函数依次调用子例程等等。变量,函数的返回地址等,驻留在堆栈上。由于子例程的递归性质,堆栈溢出。

    【讨论】:

      【解决方案2】:

      在“calculate_result”中,最后一行再次调用“calculate_result”。将行更改为:

      return (this_result)

      子程序的最后一行只是再次调用子程序,又调用了子程序,又调用了子程序,又调用了子程序,又调用了子程序……

      我想你明白了 - AppleScript,正如你所写的那样 - 崩溃,因为它只是不断调用自己,并最终耗尽内存,从而触发堆栈溢出错误。

      每当程序用尽某种内存空间时,就会发生堆栈溢出错误——它不是 AppleScript 特有的——它可以在任何编程语言中发生。有关堆栈溢出错误的更深入解释,请参阅此答案:

      (What is a stack overflow?)

      【讨论】:

        【解决方案3】:

        摘自an answer to a similar question...

        参数和局部变量在堆栈上分配(对象存在于堆上的引用类型和引用该对象的变量)。堆栈通常位于地址空间的上端,当它用完时,它会朝向地址空间的底部(即朝向零)。

        您的进程也有一个堆,它位于进程的底部。当你分配内存时,这个堆会增长到地址空间的上端。如您所见,堆有可能与堆栈“碰撞”(有点像技术板!!!)。

        堆栈溢出错误意味着堆栈(您的子例程)溢出(自身执行了很多次以至于崩溃)。堆栈溢出错误通常是由错误的递归调用引起的(对于 AppleScript,是错误的子例程调用)。

        一般来说,如果您的子例程返回值,请确保该值不是子例程名称。否则堆栈将溢出,导致程序崩溃(如果 return 语句不在 try 块内)。只需改变这个:

        return calculate_result(this_result)
        

        ...到这里

        return this_result
        

        ...你应该很高兴!

        在某些情况下,可以返回子例程名称,但前提是存在终止条件。例如,如果用户输入了无效的数字,子程序可以重新运行自己,但只有在数字无效时(如下所示):

        on get_input()
            set this_number to null
            try
                set this_number to the text returned of (display dialog "Please enter a number:" default answer "") as number
            on error --the user didn't enter a number and the program tried to coerce the result into a number and threw an error, so the program branches here
                return get_input()
            end try
            return this_number
        end get_input
        

        在上述情况下,终止条件发生在用户输入实际数字时。您通常可以判断程序何时会抛出 Stack Overflow 错误,因为没有终止条件。

        希望这些信息对您有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-15
          • 2011-12-19
          • 2011-12-26
          • 2015-08-23
          • 1970-01-01
          相关资源
          最近更新 更多