因此,訪問類型上的Ada編譯器,原來是2005年的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
;通過使用池訪問,我告訴編譯器指針不能指向堆棧對象。這工作正常。
然而,然後我嘗試使用一個名爲一般訪問:
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](https://libre2.adacore.com/download)網站下載GNAT GPL 2013。 –
我實際上在http://ideone.com上使用在線代碼片段執行環境,所以我無法控制它使用的編譯器。我知道自己做一件很容易的事,但現在,爲了我在做的事,在線的更容易。 –