如何在 ATS2 中释放字符串

  • 本文关键字:释放 字符串 ATS2 ats
  • 更新时间 :
  • 英文 :


http://www.ats-lang.org/Documents.html 包括 "ATS 编程简介",其中包括 fileref_get_line_string 返回Strptr1的断言(查看filebas.dats显示它通过 strptr2string 返回String(,它包含以下代码:

#include "share/atspre_staload.hats"
#include "share/atspre_staload_libats_ML.hats"
implement main0() = loop() where
  fun loop(): void = let
      val isnot = fileref_isnot_eof(stdin_ref)
    in
    if isnot then let
      val line = fileref_get_line_string(stdin_ref)
      val () = print_string(line)
      val () = strptr_free(line)
    in
      loop()
    end else ()
  end
end

如果包含strptr_free行,则会引发类型错误。如果未包含该行,则程序会公然泄漏内存。是否有当前文档或是否有 ATS2 示例显示应如何使用 fileref_* 单词?上面代码的 ATS2 版本是什么?

fileref_get_line_string有两个版本:一个在前奏/文件巴斯和另一个在libats/ML/filebas中。要获得线性字符串,您需要前者:

#include
"share/atspre_staload.hats"
implement
main0() = loop() where
  fun
  loop(): void = let
    val
    isnot =
    fileref_isnot_eof(stdin_ref)
  in
    if isnot then let
      val line =
      fileref_get_line_string(stdin_ref)
      val () =
      print_strptr(line)
      val () = free(line)
    in
      loop()
    end else ()
  end
end

最新更新