comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <bmoore.ada@gmail.com>
Subject: Re: Error: "non-local pointer cannot point to a local object"
Date: Tue, 11 Sep 2018 18:49:42 -0700 (PDT)
Date: 2018-09-11T18:49:42-07:00	[thread overview]
Message-ID: <04d2775f-8a9a-4280-be82-82f7ca70044d@googlegroups.com> (raw)
In-Reply-To: <7a6b1ae7-f142-458d-ab01-c36c8ed30659@googlegroups.com>

On Tuesday, September 11, 2018 at 4:21:00 PM UTC-6, 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 >>
>                 
> 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!

One solution would be to use access discriminants.


eg.
package P is
                
   type Master_Record is record 
        
      Root  : aliased Integer; 
      -- Here is where other general variables are kept 
                
   end record; 
   
   type Working_Record (Root : not null access Integer) is record 
      null;
      -- Here is where specific instance variables are kept 
   end record; 
   
end P;

with P; use P;

procedure Main is
   MR : aliased Master_Record;
   WR1 : Working_Record (Root => MR.Root'Access);
   WR2 : Working_Record (Root => MR.Root'Access);
begin   
   WR1.Root.all := 35;
   WR2.Root.all := 36;
end Main;

regards,

Brad


  reply	other threads:[~2018-09-12  1:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-11 22:20 Error: "non-local pointer cannot point to a local object" NiGHTS
2018-09-12  1:49 ` Brad Moore [this message]
2018-09-12 14:41   ` NiGHTS
2018-09-12  6:05 ` rakusu_klein
2018-09-12 14:42   ` NiGHTS
2018-09-12  7:26 ` Dmitry A. Kazakov
2018-09-12 14:47   ` NiGHTS
2018-09-12 15:22     ` 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