gpt4 book ai didi

ada - 非本地指针不能指向本地对象

转载 作者:行者123 更新时间:2023-12-04 01:58:37 26 4
gpt4 key购买 nike

为什么以下行为像它的行为:

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

没有 ArrArr_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 .

关于ada - 非本地指针不能指向本地对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com