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=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4947e94bd021c540 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-07 13:00:09 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: C array to Ada pointer to unconstrained array without copying memory References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 07 Oct 2003 20:00:09 GMT NNTP-Posting-Host: 63.184.16.136 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.pas.earthlink.net 1065556809 63.184.16.136 (Tue, 07 Oct 2003 13:00:09 PDT) NNTP-Posting-Date: Tue, 07 Oct 2003 13:00:09 PDT Xref: archiver1.google.com comp.lang.ada:394 Date: 2003-10-07T20:00:09+00:00 List-Id: Duncan Sands wrote: > [have Length and Ptr] > > subtype Array2 is Ada_Array (1 .. Length); > > X : Array2; > for X'Address use Ptr; -- ok, Ptr should be of type System'Address, but hey! You should also have pragma Import (Ada, X); to avoid initializing X. But this may not provide usable bounds for X. If, for example, your compiler stores the bounds in memory just before the 1st element of X, this may result in garbage for the bounds of X. The general technique is to use Ptr as an access value to something as large or larger than Length can ever be, then immediately take a slice and pass it elsewhere. Slicing generates the bounds information. The following compiles: procedure Bound_Generation is type T is new Integer; type Real is array (Positive range <>) of T; subtype Huge is Real (Positive); type Huge_Ptr is access all Huge; pragma Convention (C, Huge_Ptr); X_Ptr : Huge_Ptr; Length : Positive; procedure Deal_With_X (X : in Real) is begin -- Deal_With_X null; end Deal_With_X; begin -- Bound_Generation -- Get X_Ptr and Length from C Deal_With_X (X => X_Ptr (1 .. Length) ); end Bound_Generation; > And then you can make use of X. However being defined on the stack, it > can be quite awkward to use. It would be nice to have the same thing but > with X dynamically allocated. For example, There is generally no need for a pointer to X; I'd have to know more about what you're trying to do to understand why you say you want one. With GNAT, you could use 'Unrestricted_Access on X within Deal_With_X to obtain such a pointer. Or, instead of calling a subprogram, you could do something like: declare type Real_Ptr is access all Real; X : Real_Ptr; Result : aliased Real := X_Ptr (1 .. Length); begin X := Result'Access; -- Deal with X here end; -- Jeff Carter "Your mother was a hamster and your father smelt of elderberries." Monty Python & the Holy Grail 06