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,6b3a3c920575b35a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!b28g2000cwb.googlegroups.com!not-for-mail From: "Jerry" Newsgroups: comp.lang.ada Subject: Re: How to pass two dimensional arrays to C Date: 28 Jul 2006 13:46:03 -0700 Organization: http://groups.google.com Message-ID: <1154119563.642347.13670@b28g2000cwb.googlegroups.com> References: <1154084819.088112.325730@p79g2000cwp.googlegroups.com> NNTP-Posting-Host: 71.35.35.3 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1154119568 16470 127.0.0.1 (28 Jul 2006 20:46:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 28 Jul 2006 20:46:08 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: b28g2000cwb.googlegroups.com; posting-host=71.35.35.3; posting-account=Ax24hA0AAABV39UFqUVhb0kauOuAbI3T Xref: g2news2.google.com comp.lang.ada:6002 Date: 2006-07-28T13:46:03-07:00 List-Id: Thanks for everyone's comments. I have thin bindings to PLplot nearly complete except for this nagging 2D array thing. (I also have a partial thick binding plus some easy-to-use one-line plotters as well.) I understand how C passes arrays and for 1D arrays and I have relied on GNAT's near-universal use of C calling conventions--I think that this limits portability to other Ada compilers but "walk before you run" seems apropos here since I'm learning Ada and bits of C as I go along. I may later use Interfaces.C if there is sufficient interest. I have relied on these excerpts from the usual references: >>From the GNAT User's Guide, Calling Conventions: "C. Data will be passed according to the conventions described in section B.3 of the Ada 95 Reference Manual." >>From RM B.3: "An Ada parameter of an array type with component type T, of any mode, is passed as a t* argument to a C function, where t is the C type corresponding to the Ada type T." >>From the GNAT Reference Manual section 10.1: Ada scalar types match C scalar types. Also, "Ada arrays map directly to C arrays." There is no mention of 2D arrays specifically. So I'm thinking that GNAT's calling conventions work for only 1D arrays and that I'll have to figure out how to convert my 2D z array of PL_Float_Array_2D into an array of pointers each pointing to a row (column?) of z. I would want to do this without having to bother the user with defining his 2D array as a 1D array of 1D arrays. It seems that I "should" be able to simply pass a pointer (access variable) to the first location of z, that is, a pointer to (z'First(1), z'First(2), but I don't see how this could work if C in fact sees an array of pointers. Wouldn't C's representation of the array z M x N be, say, M pointers each pointing to the address of the first element of its M rows? But this would require (M+1)N storage locations which seems wrong. I would expect that C would simply expect a blob of MN units of memory where each unit is the sizeof(int) and rely on M and N being passed as parameters to parse it up correctly. I've tried to avoid using SWIG as it represents another layer of stuff that I don't know much about and because it is apparently incomplete for Ada, but as Jeffrey suggested, even though it is incomplete it might output some useful results. I'll look into Dmitry's and Adam's suggestions as well--I don't see how to make the wrapper for plmesh to minimally affect the user. I want to keep the Ada facade on the user's array types, and keep the subtype-ing from Long_Float_Array_2D or similar, whatever is provided by the user as the base type. The overall thing I have to figure out, I think, is how to convert a subtype of Long_Float_Array_2D such as PL_Float_Array_2D into an array of pointers. Jerry