From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Calling Ada from C
Date: 22 Feb 2007 09:28:31 -0800
Date: 2007-02-22T09:28:31-08:00 [thread overview]
Message-ID: <1172165311.481962.227160@q2g2000cwa.googlegroups.com> (raw)
In-Reply-To: <1172159208.098190.143360@t69g2000cwt.googlegroups.com>
On Feb 22, 7:46 am, "hannibal.h...@gmail.com"
<hannibal.h...@gmail.com> wrote:
> I have a slight problem trying to call an Ada function from a C
> function. I need to pass in an unconstrained array to the Ada
> function. The problem is how I specify the size.
>
> This is probably very simple, but I am more or less just getting
> started with Ada, comming from a C-background.
>
> My code yealds warnings like this (when compiled with GNAT):
>
> foo.ads:50:23: warning: type of argument "Insert_C.Packet" is
> unconstrained array
> foo.ads:50:23: warning: foreign caller must pass bounds explicitly
>
> I have been searching a lot in order to figure out what to do about
> this, but all the FFI documentation is for calling C-functions from
> Ada, and some very simple examples of how to call ada functions that
> take primitive arguments (ints, and similar items).
>
> Anyone who know how to pass in the bounds explicitly?
Don't use an unconstrained array. If I were to write an procedure P
to be called from C that would take, say, an unknown-size array of
integers, I'd do it like this:
type Int_Array is array (Integer range <>) of Integer;
-- this is the unconstrained array
subtype Constrained_Int_Array is Int_Array (0 .. Integer'Last);
-- or 1 .. Integer'Last, if you prefer
procedure P (Arr : in out Constrained_Int_Array; Len : in
Integer);
Then, inside P, just make sure you use Len; don't use Arr'Range or
Arr'Last which will be useless. Not the safest thing, since now your
Ada program won't do the automatic bounds checking, but it's portable.
Or, you could write a second Ada routine to do all the work:
procedure P2 (Arr : in Int_Array); -- use the unconstrained array
and make P simply a wrapper:
procedure P (Arr : in out Constrained_Int_Array; Len : in Integer)
is
begin
P2 (Arr (Arr'First .. Arr'First + Len - 1));
end P;
When you pass unconstrained arrays around as parameters from one Ada
subprogram to another, the subprograms will know the Ada conventions
for passing those arrays around. A C program won't know how to do
this, unless you find out how your particular Ada implementation does
this and make your C program set up the data in the same way, but that
will make your program non-portable. That's why I would stick to
constrained arrays when calling Ada from non-Ada (or vice versa).
-- Adam
next prev parent reply other threads:[~2007-02-22 17:28 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-02-22 15:46 Calling Ada from C hannibal.holm
2007-02-22 16:17 ` Ludovic Brenta
2007-02-22 17:28 ` Adam Beneschan [this message]
2007-02-22 20:40 ` Aurele
2007-02-23 13:53 ` Stephen Leake
2007-02-27 12:49 ` hannibal.holm
2007-02-27 18:03 ` Adam Beneschan
2007-02-28 2:54 ` Jeffrey R. Carter
-- strict thread matches above, loose matches on Subject: below --
2000-08-25 0:00 Maxwelton
1996-10-29 0:00 How is an ADA compiler done? Robert Dewar
1996-11-08 0:00 ` calling ADA from C EDSTAM Mikael
1996-11-14 0:00 ` Robert Dewar
1995-03-22 13:26 Calling Ada " Roger L Costello
1995-03-22 13:58 ` David Paton
1995-03-23 17:22 ` Theodore Dennison
1995-03-24 17:14 ` Larry Kilgallen, LJK Software
1995-03-26 11:53 ` Robert Dewar
1995-03-27 14:47 ` Theodore Dennison
1995-03-28 0:00 ` Cyrille Comar
1995-03-28 0:00 ` Robert Dewar
1995-03-29 2:47 ` Larry Kilgallen, LJK Software
1995-03-29 0:00 ` Theodore Dennison
1995-04-04 0:00 ` Robert Dewar
1995-03-27 19:48 ` Robert I. Eachus
1995-03-29 0:00 ` Larry Kilgallen, LJK Software
1991-02-23 16:01 calling " David B Lightstone
1988-10-16 0:23 calling ada from c Maureen Cragg
1988-10-17 17:21 ` Maureen Cragg
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox