comp.lang.ada
 help / color / mirror / Atom feed
* non-local pointer cannot point to local object
@ 2018-02-23  8:54 artium
  2018-02-23  9:22 ` J-P. Rosen
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: artium @ 2018-02-23  8:54 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23  8:54 non-local pointer cannot point to local object artium
@ 2018-02-23  9:22 ` J-P. Rosen
  2018-02-24  8:13   ` gautier_niouzes
  2018-02-23  9:42 ` Dmitry A. Kazakov
  2018-02-23 12:19 ` AdaMagica
  2 siblings, 1 reply; 7+ messages in thread
From: J-P. Rosen @ 2018-02-23  9:22 UTC (permalink / raw)


Le 23/02/2018 à 09:54, artium@nihamkin.com a écrit :
> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23  8:54 non-local pointer cannot point to local object artium
  2018-02-23  9:22 ` J-P. Rosen
@ 2018-02-23  9:42 ` Dmitry A. Kazakov
  2018-02-23 12:19 ` AdaMagica
  2 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-23  9:42 UTC (permalink / raw)


On 23/02/2018 09:54, artium@nihamkin.com wrote:

> 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.

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23  8:54 non-local pointer cannot point to local object artium
  2018-02-23  9:22 ` J-P. Rosen
  2018-02-23  9:42 ` Dmitry A. Kazakov
@ 2018-02-23 12:19 ` AdaMagica
  2018-02-23 16:56   ` Mehdi Saada
  2 siblings, 1 reply; 7+ messages in thread
From: AdaMagica @ 2018-02-23 12:19 UTC (permalink / raw)


Am Freitag, 23. Februar 2018 09:54:28 UTC+1 schrieb art...@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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23 12:19 ` AdaMagica
@ 2018-02-23 16:56   ` Mehdi Saada
  2018-02-23 17:16     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 7+ messages in thread
From: Mehdi Saada @ 2018-02-23 16:56 UTC (permalink / raw)


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 ?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23 16:56   ` Mehdi Saada
@ 2018-02-23 17:16     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-23 17:16 UTC (permalink / raw)


On 2018-02-23 17:56, Mehdi Saada wrote:
> type POINTER is access all INTEGER;
> Long: POINTER

   procedure Copy (X : POINTER) is
   begin
       Long := X;
   end Copy;

> declare
>     Local: aliased INTEGER := -42;
>     SHORT: POINTEUR := LOCAL'Access;
> begin
>     Long := LOCAL'Access;

       Copy (Short);

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: non-local pointer cannot point to local object
  2018-02-23  9:22 ` J-P. Rosen
@ 2018-02-24  8:13   ` gautier_niouzes
  0 siblings, 0 replies; 7+ messages in thread
From: gautier_niouzes @ 2018-02-24  8:13 UTC (permalink / raw)


Le vendredi 23 février 2018 10:34:16 UTC+1, J-P. Rosen a écrit :
> 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-02-24  8:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-23  8:54 non-local pointer cannot point to local object artium
2018-02-23  9:22 ` J-P. Rosen
2018-02-24  8:13   ` gautier_niouzes
2018-02-23  9:42 ` Dmitry A. Kazakov
2018-02-23 12:19 ` AdaMagica
2018-02-23 16:56   ` Mehdi Saada
2018-02-23 17:16     ` Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox