Ada 非本地指针不能指向本地对象

Ada 非本地指针不能指向本地对象,ada,Ada,为什么以下行为与它的行为相似: with Interfaces.C; with Interfaces.C.Strings; procedure X is type Integer_Access is access all Integer; Arr_Access : Interfaces.C.Strings.char_array_access; Arr : aliased Interfaces.C.char_array := Interfaces.C.To_C ("From"

为什么以下行为与它的行为相似:

with Interfaces.C;
with Interfaces.C.Strings;
procedure X is

   type Integer_Access is access all Integer;

   Arr_Access : Interfaces.C.Strings.char_array_access;
   Arr : aliased Interfaces.C.char_array := Interfaces.C.To_C ("From");

   A : Integer_Access;
   I : aliased Integer := 6;

begin

   Arr_Access := Arr'Access;
   A := I'Access;

end X;
结果:

$ gnatmake x.adb 
gcc -c x.adb
x.adb:16:18: non-local pointer cannot point to local object
gnatmake: "x.adb" compilation error

Arr
Arr\u Access
是否具有相同的可访问性级别?

可访问性规则是设计的()

[以]确保该对象的寿命至少与访问类型相同,从而确保访问值以后不能指定不再存在的对象

在您的情况下,访问类型是在库级别声明的,但是被访问的对象是本地的;因此,访问值有可能比Arr_access更长寿(例如,通过传递给存储它的子程序)

ARM后面有一条语句,您可以使用
'Unchecked\u Access