【发布时间】:2014-04-19 11:22:10
【问题描述】:
因此,当在 Ada 编译器(结果是 Ada 2005)上使用访问类型时,我尝试了以下经典示例:
type Node is record
next: access Node;
end record;
function reverselist(input: access Node) return access Node is
result: access Node := null;
this: access Node := input;
next: access Node := null;
begin
while this /= null loop
next := this.next;
this.next := result; -- [*]
result := this;
this := next;
end loop;
return result; -- [*]
end;
这两条带星号的行会产生编译错误,因为 Ada 2005 的分析认为我可能会泄漏本地指针。 (我知道 Ada 2012 有更好的分析并会接受这一点。但是,我没有 Ada 2012 编译器。)
好的,所以这可以通过使用命名池访问而不是匿名access Node 来轻松解决;通过使用池访问,我告诉编译器指针不能引用堆栈对象。效果很好。
不过,后来我尝试使用命名的 general 访问:
type Node;
type List is access Node;
type Node is record
next: List;
end record;
function reverselist(input: List) return access Node is
result: List := null;
this: List := input;
next: List := null;
begin
while this /= null loop
next := this.next;
this.next := result;
result := this;
this := next;
end loop;
return result;
end;
一般访问类型可以指向栈对象和堆对象;那么为什么编译器不拒绝第二个示例,原因与拒绝第一个示例的原因相同?
(我也有点惊讶匿名access integer 变量似乎以一般访问语义结束。为什么我不必使用access all integer 进行匿名一般访问?)
【问题讨论】:
-
只是想知道:什么 Ada 编译器?获取 Ada 2012 编译器很容易,只需从 AdaCore Libre 站点下载 GNAT GPL 2013。
-
我实际上在使用ideone.com 的在线代码sn-p 执行环境,所以我无法控制它使用的编译器。我知道自己买一个很容易,但现在,就我所做的而言,在线的更容易。