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-Thread: 103376,3332d83c4f455808 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s72.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Thunderbird 1.5.0.8 (Windows/20061025) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to use Annex G.3 Vectors and Matrices in bindings to C arrays References: <1163303134.998680.268570@h54g2000cwb.googlegroups.com> <1163416802.221321.170270@f16g2000cwb.googlegroups.com> In-Reply-To: <1163416802.221321.170270@f16g2000cwb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.201.97.213 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s72 1163444950 12.201.97.213 (Mon, 13 Nov 2006 19:09:10 GMT) NNTP-Posting-Date: Mon, 13 Nov 2006 19:09:10 GMT Date: Mon, 13 Nov 2006 19:09:10 GMT Xref: g2news2.google.com comp.lang.ada:7430 Date: 2006-11-13T19:09:10+00:00 List-Id: Jerry wrote: >> > I'm afraid I'll have to apologize for not being accurate in my initial > post. Indeed, I am not having any problems (as I erroneously claimed) > with 1D arrays. The problem arises with 2D arrays, about which I > pestered the list earlier. With further apologies for posting a bit > more code that I would like, the following is a typical binding that I > made in which it was necessary to gain access to 2D C arrays. OK. > z : in out Long_Float_Array_2D; -- height of surface > at definition points I don't see why this is in out. > package PL_Float_Pointers_Local is new Interfaces.C.Pointers > (Index => Integer, > Element => Long_Float, > Element_Array => Long_Float_Array_1D, > Default_Terminator => 0.0); > use type PL_Float_Pointers_Local.Pointer; -- as in RM B.3.2 > type PL_Float_Pointer_Array_Local is array (Integer range <>) of > PL_Float_Pointers_Local.Pointer; -- array of pointers to Long_Floats > which represent the first element of each row of z in C-land > > Index_Of_First_Column : Integer := z'First(2); > z_As_Pointers : PL_Float_Pointer_Array_Local (z'range(1)); > > procedure > plmeshc_local(x : PL_Float_Array; y : PL_Float_Array; z : > PL_Float_Pointer_Array_Local; nx : Integer; ny : Integer; opt : > Integer; clevel : PL_Float_Array; nlevel : PLINT); > pragma Import(C, plmeshc_local, "c_plmeshc"); > > begin > for Index in z'range(1) loop > z_As_Pointers(Index) := z(Index, Index_Of_First_Column)'access; To do this, Z must have aliased components. If you want to use the language-defined matrix type for this, you'll have to copy the values from Z into a local array. I'd probably do something like type Row_Of_Z is new Vector (Z'range (2) ); pragma Convention (C, Row_Of_Z); type Row_Ptr is access all Row_Of_Z; pragma Convention (C, Row_Ptr); type Z_For_C is array (Z'range (1) ) of Row_Ptr; pragma Convention (C, Z_For_C); Z_Ptr : Z_For_C; for I in Z_Ptr'range loop Z_Ptr := new Row_Of_Z; for J in Z'Ptr (I)'range loop Z_Ptr (J) := Z (I, J); end loop; end loop; -- Call C with Z_Ptr -- Free Z_Ptr You could, of course, put the rows into an array of aliased Row_Of_Z and use the 'access of its elements to fill Z_Ptr. That would eliminate the need to free the pointers. A messy way, that doesn't involve copying the elements, is to instantiate System.Address_To_Access_Conversions, and derive a convention-C access type from the access type defined in the instantiation. Then you take 'Address, convert it to an access type using To_Pointer, then convert that to your convention-C access type and store it in your array of pointers. (This is equivalent to using GNAT's 'Unrestricted_Access, but portable.) > The related array definitions are: > type Long_Float_Array_1D is array (Integer range <>) of aliased > Long_Float; > type Long_Float_Array_2D is array (Integer range <>, Integer range > <>) of aliased Long_Float; > > The "thin" binding Ada procedure plmeshc is defined: > procedure plmeshc(x : PL_Float_Array; y : PL_Float_Array; z : > PL_Float_Array_2D; nx : PLINT; ny : PLINT; opt : PLINT; > clevel : PL_Float_Array; nlevel : PLINT); > pragma Import(C, plmeshc, "c_plmeshc"); The declarations of the PL_... types would be useful, too, though we can make a good guess from the C. -- Jeff Carter "Now go away or I shall taunt you a second time." Monty Python & the Holy Grail 07