Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-local pointer cannot point to local object

Tags:

ada

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?

like image 209
Artium Avatar asked Feb 23 '18 01:02

Artium


1 Answers

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.

like image 166
Simon Wright Avatar answered Sep 23 '22 06:09

Simon Wright