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!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Error: "non-local pointer cannot point to a local object" Date: Wed, 12 Sep 2018 09:26:44 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <7a6b1ae7-f142-458d-ab01-c36c8ed30659@googlegroups.com> NNTP-Posting-Host: MyFhHs417jM9AgzRpXn7yg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:54330 Date: 2018-09-12T09:26:44+02:00 List-Id: On 2018-09-12 00:20, NiGHTS wrote: > As the subject line indicates, I am having a compile error which I am having difficulty solving. > > I have two distinct records, in this example called "Master_Record" and "Working_Record". One contains an Integer, the other a "pointer" to that Integer. In this context, Working_Record depends on Master_Record. There can be many Working_Record instances for a single Master_Record. The Copy function is intended to enforce this rule by acting as an object factory, requiring an instance of Master_Record to create Working_Record. I know this isn't perfect, so any suggestions on a better strategy would be appreciated. > > Ultimately the purpose of this is for Working_Record to be a copy-friendly record, where Master_Record is not intended to be used directly, except as a place to anchor the variable scope. The resulting code will be written as a library object containing two records just like I have here, as well as a Copy function. The user of this library object is expected to create an instance of Master_Record, then copy it to Working_Record. > > ---------------------------------------- > type Master_Record is record > > Root : aliased Integer; > -- Here is where other general variables are kept > > end record; > > type Working_Record is record > > Root_Pointer : access Integer; > -- Here is where specific instance variables are kept > > end record; > > function Copy ( > > Source : in out Master_Record > > ) return Working_Record is > begin > > return (Root_Pointer => Source.Root'Access); -- << Error here >> What about killing accessibility info: return (Root_Pointer => Source.Root.all'Unchecked_Access); > end Copy; > > MR : Master_Record; > WR : Working_Record := Copy (MR); > ---------------------------------------- > > Any ideas on how to achieve this goal? I'm sure its something simple. Thanks in advance! What goal? It looks like a bad design. Copying pointers to integers is asking for trouble. BTW, if you used Ada.Finalization.Controlled, as you likely should, and derived one record from another then default copying before Adjust would copy pointer too. What would happen with access info, I have no idea, probably copied as-is. (Yet another reason not to use anonymous access types inside records unless for Rosen's trick or access-to-constant) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de