comp.lang.ada
 help / color / mirror / Atom feed
From: "Jerry" <lanceboyle@qwest.net>
Subject: Re: How to pass two dimensional arrays to C
Date: 28 Jul 2006 13:46:03 -0700
Date: 2006-07-28T13:46:03-07:00	[thread overview]
Message-ID: <1154119563.642347.13670@b28g2000cwb.googlegroups.com> (raw)
In-Reply-To: 1154084819.088112.325730@p79g2000cwp.googlegroups.com

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




  parent reply	other threads:[~2006-07-28 20:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-28 11:06 How to pass two dimensional arrays to C Jerry
2006-07-28 12:27 ` Jeffrey Creem
2006-07-28 12:27 ` Dmitry A. Kazakov
2006-07-28 16:53 ` Adam Beneschan
2006-07-28 20:15 ` Jeffrey R. Carter
2006-07-28 20:46 ` Jerry [this message]
2006-07-28 21:14   ` Jeffrey Creem
2006-07-28 22:54   ` Björn Persson
2006-07-29  1:14     ` Jeffrey R. Carter
2006-07-29  7:11       ` Simon Wright
2006-07-29 22:12         ` Jeffrey R. Carter
2006-07-30 11:18           ` Simon Wright
2006-07-30 11:20           ` Simon Wright
2006-07-29  4:19   ` REH
2006-07-29  4:28     ` REH
2006-07-29  4:30       ` REH
2006-08-14  6:59     ` Dave Thompson
2006-07-29  5:47 ` REH
2006-08-02  8:20 ` Jerry
2006-08-02  9:03   ` Dmitry A. Kazakov
2006-08-02 10:22     ` Jerry
2006-08-02 18:25   ` Björn Persson
2006-08-05  1:03 ` Jerry
replies disabled

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