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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM 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!postnews.google.com!u6g2000prc.googlegroups.com!not-for-mail From: Adrian Hoe Newsgroups: comp.lang.ada Subject: Re: Ada array vs C pointer (call by reference) Date: Fri, 27 Jun 2008 01:22:01 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 218.208.231.125 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1214554921 23007 127.0.0.1 (27 Jun 2008 08:22:01 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 27 Jun 2008 08:22:01 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u6g2000prc.googlegroups.com; posting-host=218.208.231.125; posting-account=coq9PAkAAAB2Xx46RZLFJw5dY9DVXW4- User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:906 Date: 2008-06-27T01:22:01-07:00 List-Id: On Jun 27, 12:10 pm, "Jeffrey R. Carter" wrote: > Adrian Hoe wrote: > > > 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? > > type C_Data is array (Positive range <>) of Interfaces.C.C_Float; > pragma Convention (C, C_Data); Can I use Float instead of Interfaces.C.C_Float? One question has been raised to me: Why use Interfaces.C.Int, Interfaces.C.C_Float since they are all new declaration of Ada types. I know it is for readability and maintainability but the question is logical as the declaration in Interfaces.C is just merely creating a new Ada type with a new name, unless there is pragma Convention statement after every such declaration in Interfaces.C. So, that leads to another question: Wouldn't it be better to write as such? type C_Data is array (Positive range <>) of Float; pragma Convention (C, C_Data); > function PWM_Read (Count : in Interfaces.C.Int; Data : in C_Data) > return Interfaces.C.Int; > pragma Import (C, PWM_Read, "pwmRead"); > > PWM_Count : constant := ...; > > PWM_Data : C_Data (1 .. PWM_Count); > Result : Interfaces.C.Int; > > Result := PWM_Read (PWM_Count, PWM_Data); Is PWM_Data passed as pointer to pwmRead? Thanks again. -- Adrian Hoe http://adrianhoe.com