From: "J-P. Rosen" <rosen@adalog.fr>
Subject: Re: non-local pointer cannot point to local object
Date: Fri, 23 Feb 2018 10:22:05 +0100
Date: 2018-02-23T10:22:05+01:00 [thread overview]
Message-ID: <p6on6m$8so$1@dont-email.me> (raw)
In-Reply-To: <15e8a72b-3644-4193-8947-b0aaf587c1c2@googlegroups.com>
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
next prev parent reply other threads:[~2018-02-23 9:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-23 8:54 non-local pointer cannot point to local object artium
2018-02-23 9:22 ` J-P. Rosen [this message]
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox