需要修复来自外星来源的非常庞大的 ocaml 程序。问题之一是程序在看似无辜的行崩溃:
Hashtbl.mem loc_to_no loc
“内存不足”异常。问题是那里肯定有足够的内存,并且这条线对于其他输入来说执行得很好,甚至在处理问题的过程中更早。
使用
OCAMLRUNPARAM="v=63"
运行它,我看到在崩溃之前打印的一行:Stack overflow in structural comparison
所涉及的结构定义如下。
loc
是 location
类型.type ('a, 'b, 'c) automaton = {
aut_id : int ;
mutable start_location : (('a, 'b, 'c) location) option ;
mutable end_location : (('a, 'b, 'c) location) option ;
mutable aut_attributes : 'a ;
}
and ('a, 'b, 'c) location = {
loc_id : int ;
mutable succs : ('c * ('a, 'b, 'c) location) list ;
mutable preds : ('c * ('a, 'b, 'c) location) list ;
automaton : ('a, 'b, 'c) automaton ;
mutable loc_attributes : 'b ;
}
应该怎么做才能使代码执行?
最佳答案
嗯,哈希表查找使用“=
”(结构相等)来确定键是否与您要查找的键相同。结构平等需要深入检查所有子结构和东西。你有一个复杂的递归数据结构。也许在某个时候结构中有一个循环,这会导致它无限循环。在任何情况下,考虑一下您希望哈希表如何判断一个键是否与您的相同,然后您需要使用这种相等而不是默认的结构相等;使用 Hashtbl.Make
仿函数来定义具有自定义相等和/或散列函数的散列表模块。
关于crash - Ocaml "Out of memory"异常;详细模式说 "Stack overflow in structural comparison",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1393233/