非局部指针不能指向局部对象
non-local pointer cannot point to local object
为什么下面的行为像它的行为:
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; -- not OK
A := I’Access; -- OK
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_Access
的无障碍级别不一样吗?
无障碍规则设计完成(ARM 3.10.2(3))
[to] ensure[s] that the object will live at least as long as the access type, which in turn ensures that the access value cannot later designate an object that no longer exists.
在你的例子中,访问类型是在库级别声明的,但被访问的对象是本地的;因此访问值有可能比 Arr_Access
长寿(例如,通过传递给存储它的子程序)。
ARM 紧接着声明您可以使用 ’Unchecked_Access
。
为什么下面的行为像它的行为:
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; -- not OK
A := I’Access; -- OK
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_Access
的无障碍级别不一样吗?
无障碍规则设计完成(ARM 3.10.2(3))
[to] ensure[s] that the object will live at least as long as the access type, which in turn ensures that the access value cannot later designate an object that no longer exists.
在你的例子中,访问类型是在库级别声明的,但被访问的对象是本地的;因此访问值有可能比 Arr_Access
长寿(例如,通过传递给存储它的子程序)。
ARM 紧接着声明您可以使用 ’Unchecked_Access
。