comp.lang.ada
 help / color / mirror / Atom feed
* Array access via pointer?
@ 2000-08-01  0:00 Michael Carman
  2000-08-01  0:00 ` Ted Dennison
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Michael Carman @ 2000-08-01  0:00 UTC (permalink / raw)


First, a warning: my knowledge of Ada is pretty rudimentary, and I tend
to use the terms I'm familiar with from other languages. If words like
"pointer" and "reference" betray that background, oh well.

Quick overview:
I have several arrays, each holding the same type of data (system 
addresses) but the arrays are of different sizes. I want to have a 
single procedure to process the data in these arrays. (For various 
reasons, the numbers/sizes of arrays are necessary, so no paradigm 
shifts allowed there!) In order to do this I pass my procedure a
pointer to an array and an index into it. It looks something like
this:

type Addr_Table is array (Natural range <>) of System.Address;

Arr1 : Addr_Table (0 .. 100);
Arr2 : Addr_Table (0 .. 20);

procedure Foo (Addr : in System.Address;
               I    : in Integer) is
begin
    -- Do something with index I of the array at Addr
end Foo;

A call to it might look like Foo (Arr2'Address, 7). Inside Foo, I tried 
to get at the array data in this manner:

    Data := Addr_To_Ptr(Addr).all (Idx);

where 

    type Addr_Table_Ptr is access Addr_Table;
    function Addr_To_Ptr is new Unchecked_Conversion
        (System.Address, Addr_Table_Ptr);

but that gave me a constraint error, apparently because type Addr_Table 
is unconstrained and the instantiaions of it are not. Yes, if Addr_Table
had a definate size this would be easy, but it doesn't and (for me) it's
not.

The only way I've been able to make it work is with this:

    Data := Addr_To_Ptr(Addr + Idx * Size_In_Words).all;

where

    type Addr_Ptr is access System.Address;
    function Addr_To_Ptr is new Unchecked_Conversion
        (System.Address, Addr_Ptr);
    Size_In_Words := System.Word_Size / System.Storage_Unit;

but that seems unnecessarily messy. Well, it looks like C, which 
wouldn't really bother me but I'm not writing C at the moment. It 
seems to me that there should be a way to do this without having to 
fight against the language. So, what's the canonical way of doing 
this in Ada?

-mjc




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2000-08-04  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-01  0:00 Array access via pointer? Michael Carman
2000-08-01  0:00 ` Ted Dennison
2000-08-02  0:00   ` Michael Carman
2000-08-03  0:00     ` Ken Garlington
2000-08-03  0:00       ` Robert A Duff
2000-08-03  0:00         ` Ken Garlington
2000-08-04  0:00           ` Robert I. Eachus
2000-08-04  0:00             ` Ken Garlington
2000-08-02  0:00 ` tmoran
2000-08-02  0:00   ` Michael Carman
2000-08-02  0:00     ` tmoran
2000-08-02  0:00 ` Markku Kotiaho

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