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,7661856b1d8dc0ab X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!q2g2000cwa.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Calling Ada from C Date: 22 Feb 2007 09:28:31 -0800 Organization: http://groups.google.com Message-ID: <1172165311.481962.227160@q2g2000cwa.googlegroups.com> References: <1172159208.098190.143360@t69g2000cwt.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1172165317 9977 127.0.0.1 (22 Feb 2007 17:28:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 22 Feb 2007 17:28:37 +0000 (UTC) In-Reply-To: <1172159208.098190.143360@t69g2000cwt.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: q2g2000cwa.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news2.google.com comp.lang.ada:9421 Date: 2007-02-22T09:28:31-08:00 List-Id: On Feb 22, 7:46 am, "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