comp.lang.ada
 help / color / mirror / Atom feed
From: awdorrin <awdorrin@gmail.com>
Subject: Re: Memory Access
Date: Tue, 8 Nov 2011 09:46:36 -0800 (PST)
Date: 2011-11-08T09:46:36-08:00	[thread overview]
Message-ID: <91ebbfff-9108-4401-9056-963db3f12f9e@o14g2000yqh.googlegroups.com> (raw)
In-Reply-To: b027d386-129f-41ca-ad75-23b9c106e911@t38g2000prg.googlegroups.com

On Nov 8, 11:00 am, Adam Beneschan <a...@irvine.com> wrote:
>
> You're saying that Dev_Table is a global access to an unconstrained
> array, and it sounds to me like you're trying to get Dev_Table to
> point to some arbitrary address that isn't known until runtime.
> Unfortunately, I don't think there's a portable way to do that in Ada
> (because the implementation of unconstrained arrays differs between
> implementations).  In fact, there may not be a good way to do it at
> all in GNAT, even non-portably.
>
> A possibility is to make Dev_Table an access to a *constrained*
> array.  If Num_Devs_Cnst is a constant that is known at compile time,
> you could declare
>
>   subtype Constrained_Dev_Table is Dev_Table_Type (1 ..
> Num_Devs_Cnst);
>   type Dev_Table_Ptr_Type is access all Constrained_Dev_Table;
>
> and now there are a couple ways to make Dev_Table_Ptr_Type point to
> what you want.  Here's one, based on an example I gave earlier:
>
>    with System.Address_To_Access_Conversions;
>
> then declare the following **globally** (not inside a subprogram):
>
>    package Dev_Table_Pointer is new
>          System.Address_To_Access_Conversions
> (Constrained_Dev_Table);
>
> and now, to make Dev_Table point to the data at address Dev_Data_Obj:
>
>    Dev_Table := Dev_Table_Ptr_Type (Dev_Table_Pointer.To_Pointer
> (Dev_Data_Obj));
>
> If Num_Devs_Cnst isn't known at compile time, you could still declare
> a constrained array type:
>
>   subtype Constrained_Dev_Table is Dev_Table_Type (1 .. Integer'Last);
>
> and do the same as above.  But then you have to do extra work to make
> sure that you don't later access a Dev_Table element outside the range
> 1 .. Num_Devs_Cnst.  One way is that whenever you want to work with
> Dev_Table:
>
>   declare
>      Actual_Dev_Table : Dev_Table_Type renames Dev_Table (1 ..
> Num_Devs_Cnst);
>         -- or Dev_Table.all (1 .. Num_Devs_Cnst), which means the same
> thing
>   begin
>      --
>      Something := Actual_Dev_Table (NNN).Something;
>         -- will raise Constraint_Error if NNN is out of range 1 ..
> Num_Devs_Cnst
>   end;
>
> Hope this helps.
>
>                         -- Adam


Adam, Thanks - I tried your suggestion and it looks like it may be
working. At least the code is no longer SegFaulting ;)

Now I just have to try to understand what is actually going on with
those commands.

I'm assuming that the subtype is setting the bounds on the array. so
that the Dev_Table_Ptr_Type is now able to see the elements.
The Dev_Table_Pointer.To_Pointer is mapping the address to an access
type.
I'm not quite sure I understand why the package definition for
Dev_Table_Pointer is needed - and why
System.Address_To_Access_Conversions cannot be used directly... i'm
guessing its due to Ada type checking?

Thanks!



  reply	other threads:[~2011-11-08 17:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-07 20:09 Memory Access awdorrin
2011-11-07 21:26 ` Simon Wright
2011-11-07 22:03 ` anon
2011-11-07 22:21 ` Adam Beneschan
2011-11-07 22:42   ` Adam Beneschan
2011-11-07 23:13   ` Simon Wright
2011-11-07 23:32     ` Adam Beneschan
2011-11-08 12:22       ` awdorrin
2011-11-08 16:00         ` Adam Beneschan
2011-11-08 17:46           ` awdorrin [this message]
2011-11-08 20:11             ` Adam Beneschan
2011-11-08 20:24               ` awdorrin
2011-11-09 14:42                 ` awdorrin
2011-11-08 16:10         ` awdorrin
2011-11-08 18:33           ` Simon Wright
2011-11-08 18:34             ` Simon Wright
2011-11-08 20:18             ` awdorrin
2011-11-08 12:44       ` Simon Wright
2011-11-07 22:26 ` Niklas Holsti
2011-11-07 22:53   ` Adam Beneschan
replies disabled

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