From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "J-P. Rosen" Newsgroups: comp.lang.ada Subject: Re: non-local pointer cannot point to local object Date: Fri, 23 Feb 2018 10:22:05 +0100 Organization: Adalog Message-ID: References: <15e8a72b-3644-4193-8947-b0aaf587c1c2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 23 Feb 2018 09:34:14 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="347b987524c4a651deb947603128fb29"; logging-data="9112"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18099CA70cFEZQtmiDEKA5d" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 In-Reply-To: <15e8a72b-3644-4193-8947-b0aaf587c1c2@googlegroups.com> Content-Language: fr Cancel-Lock: sha1:n0jxI7Is1vFP4vXjwrkFy0U7xtM= Xref: reader02.eternal-september.org comp.lang.ada:50590 Date: 2018-02-23T10:22:05+01:00 List-Id: 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