Discussion:
non-local pointer cannot point to local object
(too old to reply)
a***@nihamkin.com
2018-02-23 08:54:26 UTC
Permalink
I am cross costing a question from SO since it looks like all the Ada experts are here and not there :)

https://stackoverflow.com/questions/48939845/non-local-pointer-cannot-point-to-local-object


Can someone explain why I get "non-local pointer cannot point to local object" error, even though it looks like "Arr" and "Arr_Access" have the same accessibility level?

Can I overcome the problem without dynamically allocating memory and without using "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;
A := I'Access;

end X;



Thank you,
Artium
J-P. Rosen
2018-02-23 09:22:05 UTC
Permalink
Post by a***@nihamkin.com
Can someone explain why I get "non-local pointer cannot point to
local object" error, even though it looks like "Arr" and "Arr_Access"
have the same accessibility level?
Can I overcome the problem without dynamically allocating memory and without using "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;
A := I'Access;
end X;
What counts for accessibility is the place where the type of the object
is declared, not where the object itself is declared. In your case,
char_array_access is a global type, therefore you cannot assign a
pointer to a local variable. Otherwise, Arr_Access could later be
assigned to some global variable, and you would end up with a global
variable pointing to a local object. Using Unchecked_Access means that
you swear that you don't do such evil things.

Ada has this nice property that no object can survive to its type (no,
it's not obvious; I think this is not guaranteed by C++ - can anyone
confirm?). Therefore, when you exit the scope of an access type, you are
guaranteed that there is no more value of this type hanging around.
--
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr
g***@hotmail.com
2018-02-24 08:13:57 UTC
Permalink
Post by J-P. Rosen
What counts for accessibility is the place where the type of the object
is declared, not where the object itself is declared.
Then, wouldn't be a message like:

"pointer of non-local type cannot point to local object"

more helpful than

"non-local pointer cannot point to local object"

?
(I remember having been puzzled by the same thing long time ago...)

G.

Dmitry A. Kazakov
2018-02-23 09:42:44 UTC
Permalink
Post by a***@nihamkin.com
Can someone explain why I get "non-local pointer cannot point to
local object" error, even though it looks like "Arr" and "Arr_Access"
have the same accessibility level?
In the equation are the type of pointer and the target object, not two
objects.
Post by a***@nihamkin.com
Can I overcome the problem without dynamically allocating memory and without using "Unchecked_Access"?
You probably don't need char_array_access. char_array is meant to be
used where C expects char *. Just pass To_C ("from") right to C.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
AdaMagica
2018-02-23 12:19:23 UTC
Permalink
Post by a***@nihamkin.com
Can someone explain why I get "non-local pointer cannot point to local object" error, even though it looks like "Arr" and "Arr_Access" have the same accessibility level?
Just a little example:

type Pointer is access Integer;
Long: Pointer;
declare
Local: aliased Integer := -42;
Short: Pointer := Local'Access; -- illegal
begin
Long := Short;
end;
-- If this were legal, Long would point to a nonexistent object
Mehdi Saada
2018-02-23 16:56:15 UTC
Permalink
type POINTER is access all INTEGER;
Long: POINTER
declare
Local: aliased INTEGER := -42;
SHORT: POINTEUR := LOCAL'Access;
begin
Long := LOCAL'Access;
end;
...Long is a dangling reference after the "end", but SHORT never becomes one. Why must SHORT := LOCAL'Access be forbidden, can't the compiler see that no assignation to outliving variable can happen ?
Dmitry A. Kazakov
2018-02-23 17:16:14 UTC
Permalink
Post by Mehdi Saada
type POINTER is access all INTEGER;
Long: POINTER
procedure Copy (X : POINTER) is
begin
Long := X;
end Copy;
Post by Mehdi Saada
declare
Local: aliased INTEGER := -42;
SHORT: POINTEUR := LOCAL'Access;
begin
Long := LOCAL'Access;
Copy (Short);
Post by Mehdi Saada
end;
...Long is a dangling reference after the "end", but SHORT never becomes one. Why must SHORT := LOCAL'Access be forbidden, can't the compiler see that no assignation to outliving variable can happen ?
It cannot without global analysis, so a nuclear solution...
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Continue reading on narkive:
Loading...