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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5a84d5077c54a29d X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!wn14feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Ada array vs C pointer (call by reference) Reply-To: anon@anon.org (anon) References: X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Sat, 28 Jun 2008 01:21:33 GMT NNTP-Posting-Host: 12.65.204.5 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1214616093 12.65.204.5 (Sat, 28 Jun 2008 01:21:33 GMT) NNTP-Posting-Date: Sat, 28 Jun 2008 01:21:33 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:931 Date: 2008-06-28T01:21:33+00:00 List-Id: The unsafe way is to use "Address" attribute. function pwmRead ( Count : Interfaces.C.int ; Data : System.Address ) return Interfaces.C.int ; pragma Import ( C, pwmRead, "pwmRead" ) ; The standard safe method you must use two interface packages. Interfaces.C ; Interfaces.C.Pointers ; and create a C type of pointer package for the Float type, such as type Float_array is array ( Interfaces.C.size_t range <> ) of aliased float; package Float_Pointer is new Interfaces.C.Pointers ( Index => size_t, Element => Float, Element_Array => Float_array, Default_Terminator => 0 ) ; study RM B.3.2 ( 45 ) for an example that uses character array. Should be easy to adapt to Float. In , Adrian Hoe writes: >Hi, > >I have a C library function which takes a float * as a parameter. The >function is as below: > > int pwmRead (int pwmCount, float * data); > >where data is an array of float which size is determined by pwmCount. > >In Ada, I have: > > PWM_Count : Integer := No_Of_Components_Intended; > PWM_Data : array (1 .. PWM_Count) of Float; > >My concern is how safe to pass an Ada array to a C function as a >pointer (call by reference)? I presume Ada will allocate contiguous >memory for array, but what if it does not? How can we establish a >deterministic allocation and to make sure float * is accessed >contiguously? Is there any "safe" way to do this? > >Thanks. >-- >Adrian Hoe >http://adrianhoe.com