Why does the following behaves like it behaves:
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;
Result:
$ gnatmake x.adb
gcc -c x.adb
x.adb:16:18: non-local pointer cannot point to local object
gnatmake: "x.adb" compilation error
Doesn't Arr
and Arr_Access
have the same accessibility level?
The accessibility rules are designed (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.
In your case, the access type is declared at library level, but the object being accessed is local; so it would be possible for the access value to outlive Arr_Access
(by being passed to a subprogram that stores it, for instance).
The ARM follows this with the statement that you can use ’Unchecked_Access
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With