comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Memory Access
Date: Tue, 8 Nov 2011 08:00:49 -0800 (PST)
Date: 2011-11-08T08:00:49-08:00	[thread overview]
Message-ID: <b027d386-129f-41ca-ad75-23b9c106e911@t38g2000prg.googlegroups.com> (raw)
In-Reply-To: 35c7e403-6503-4e26-8764-9783caf84871@e2g2000vbb.googlegroups.com

On Nov 8, 4:22 am, awdorrin <awdor...@gmail.com> wrote:
> Something like:
>
> declare
>   tempDevTable : Dev_Table_Ptr_Type := new Dev_Table_Type( 1 ..
> Num_Devs_Cnst );
>   for tempDevTable'Address use Dev_Data_Obj;
> begin
>   Dev_Table := tempDevTable;
> end
>
> I'm thinking this would not work - too used to C pointers I suppose
> where it doesn't matter. ;)

You're right: this would *not* work.

Here's what the above code would do.  tempDevTable is a pointer
object, which may be one word or three words (or something else)
depending on the implementation.  This pointer *object* would be
located at Dev_Data_Obj---not the pointed-to data.  The "new"
operation causes the program to allocate memory from a storage pool.
The memory it allocates will be enough to hold Num_Devs_Cnst objects
of type Config_Type.  The point here is that the "new" always means to
allocate memory from a storage pool; you can't use an 'Address clause
to change the meaning of "new" to something else, which is what I
think you were trying to do.

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




  reply	other threads:[~2011-11-08 16:02 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 [this message]
2011-11-08 17:46           ` awdorrin
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