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,5a84d5077c54a29d X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!p39g2000prm.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Ada array vs C pointer (call by reference) Date: Fri, 27 Jun 2008 08:07:57 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8ea4af7b-5128-43c8-adc7-c6fcf2ba1bbc@p39g2000prm.googlegroups.com> References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1214579278 20667 127.0.0.1 (27 Jun 2008 15:07:58 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 27 Jun 2008 15:07:58 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p39g2000prm.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ 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) Xref: g2news1.google.com comp.lang.ada:912 Date: 2008-06-27T08:07:57-07:00 List-Id: On Jun 27, 1:22 am, Adrian Hoe wrote: > > 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? You could. But keep in mind that the same compiler from planet Zorxkrug that would allocate an array in a non-contiguous manner may also implement Float in a very different way than you'd expect, like maybe putting the exponent in the middle of the word instead of at the left. I mean, if you're going to worry about one you should worry about the other too for consistency. :-) By the way, although it's uncommon, I do know of one Ada compiler whose authors made the decision (in the Ada 83 days) to define Float as a 64-bit floating-point type, and Short_Float as a 32-bit type. This is of course different from C usage on the most common platforms (in which typically "float" is 32 bits and "double" is 64). So that would be one reason to use Interfaces.C.C_Float. The Ada compiler in that case should at least attempt to use the same representation that would be used for C programs; although, as Maciej pointed out, there's no guarantee that this will be correct, it should at least try. There are no such requirements for the standard "Float" type---there is no requirement or suggestion that it should conform to any particular C definition of the type. > > 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. Implicitly, there is. See B.3(42). > 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); Only if you know for certain that Float is the same type as the C "float" and don't care about portability. > > 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? If the Implementation Advice is followed, yes. See B.3(70). You might want to read this section of the RM, since you're asking several questions that the RM directly answers. -- Adam