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!s48g2000cws.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Calling Ada from C Date: 27 Feb 2007 10:03:49 -0800 Organization: http://groups.google.com Message-ID: <1172599429.504946.73760@s48g2000cws.googlegroups.com> References: <1172159208.098190.143360@t69g2000cwt.googlegroups.com> <1172580562.037493.251070@a75g2000cwd.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 1172599456 19917 127.0.0.1 (27 Feb 2007 18:04:16 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 27 Feb 2007 18:04:16 +0000 (UTC) In-Reply-To: <1172580562.037493.251070@a75g2000cwd.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: s48g2000cws.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news2.google.com comp.lang.ada:9581 Date: 2007-02-27T10:03:49-08:00 List-Id: On Feb 27, 4:49 am, "hannibal.h...@gmail.com" wrote: > On Feb 23, 2:53 pm, Stephen Leake > wrote: > > > "hannibal.h...@gmail.com" writes: > > > 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. > > > Since the concept "unconstrained array" doesn't exist in C, this can't > > possibly be a precise description of what you are doing. > > > Can you give more context, and an example of the code? Especially the > > C declarations of the data structure you are passing to Ada. > > > -- > > -- Stephe > > The data structure in C is just a pointer to a block of data to be > sent over a network link. This need to be converted to a byte array > defined as: > > Byte_Size : constant := 8; > > type Byte is range 0 .. 2**Byte_Size - 1; > for Byte'Size use Byte_Size; > > type Byte_Array is array (Natural range <>) of Byte; > pragma Pack (Byte_Array); > > As said, I am not extremly familiar with Ada and come from a C- > background, so I have a few problems trying to figure out what the ada > runtime and type system actually does. I tried something like > > -- This proc is exported as with C-conventions > procedure Some_Ada_Proc(Msg : in Byte_Array; > Len : in Natural) is > Msg_Constr : Byte_Array := Msg(0 .. Length - 1); > begin > ... > end Some_Ada_Proc; > > This is called with: > > char foo[] = {0xde, 0xad, 0xbe, 0xef}; > some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4 > > But that result in a crash which I assume is due to how Ada treats > arrays passed as arguments. No, it's due to how Ada treats *unconstrained* arrays passed as arguments. When an Ada procedure has an unconstrained array as the argument, it needs the bounds (the 'First and 'Last of the actual array), so those have to be passed in somehow. When the array parameter is declared with a *constrained* array type [technically, an array subtype], the Ada routine knows the bounds already, so they don't have to be passed in. So there normally isn't anything funny about how they get passed in; the Ada routine will normally just take the address of the first element of the array, the way you'd expect any other language to do it. So instead of making Msg a Byte_Array, which is unconstrained, make it some constrained subtype: type Byte_Array is array (Natural range <>) of Byte; pragma Pack (Byte_Array); subtype Byte_Array_Constrained is Byte_Array (Natural'First .. Natural'Last); -- pick a better name than this procedure Some_Ada_Proc (Msg : in Byte_Array_Constrained; ... Please see my earlier post for more details. -- Adam