comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Ada array vs C pointer (call by reference)
Date: Fri, 27 Jun 2008 08:07:57 -0700 (PDT)
Date: 2008-06-27T08:07:57-07:00	[thread overview]
Message-ID: <8ea4af7b-5128-43c8-adc7-c6fcf2ba1bbc@p39g2000prm.googlegroups.com> (raw)
In-Reply-To: dae8b954-fda8-42f8-8953-886896a7776e@u6g2000prc.googlegroups.com

On Jun 27, 1:22 am, Adrian Hoe <aby...@gmail.com> 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






  reply	other threads:[~2008-06-27 15:07 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-27  1:39 Ada array vs C pointer (call by reference) Adrian Hoe
2008-06-27  1:42 ` Adrian Hoe
2008-06-27  2:55 ` Adam Beneschan
2008-06-27 13:02   ` Maciej Sobczak
2008-06-27 13:15     ` Adrian Hoe
2008-06-27 14:43     ` Georg Bauhaus
2008-06-27 14:47       ` Georg Bauhaus
2008-06-27 20:35       ` Maciej Sobczak
2008-06-27 22:00         ` Georg Bauhaus
2008-06-27 22:46           ` Keith Thompson
2008-06-27 16:11     ` Keith Thompson
2008-06-27 17:00       ` Robert A Duff
2008-06-27 18:15         ` Keith Thompson
2008-06-28 14:02         ` Stephen Leake
2008-06-28 21:18           ` Keith Thompson
2008-07-03 12:44         ` Rob Norris
2008-06-27 20:44       ` Maciej Sobczak
2008-06-27 22:14         ` Keith Thompson
2008-06-27 22:36           ` Adam Beneschan
2008-06-28 14:04             ` Stephen Leake
2008-06-28 21:22               ` Keith Thompson
2008-06-30 17:13               ` Adam Beneschan
2008-06-28 17:52             ` Robert A Duff
2008-06-30 17:13               ` Adam Beneschan
2008-06-30 18:55                 ` Robert A Duff
2008-07-01 21:19                 ` Randy Brukardt
2008-07-01 21:19                 ` Randy Brukardt
2008-06-28  0:56         ` Peter C. Chapin
2008-06-28 14:11           ` Maciej Sobczak
2008-06-28 17:49             ` tmoran
2008-06-28 21:46             ` Keith Thompson
2008-06-28 17:44         ` Robert A Duff
2008-07-01 21:10       ` Randy Brukardt
2008-06-27 18:13     ` tmoran
2008-06-27 20:49       ` Maciej Sobczak
2008-06-27  4:10 ` Jeffrey R. Carter
2008-06-27  8:22   ` Adrian Hoe
2008-06-27 15:07     ` Adam Beneschan [this message]
2008-06-27 22:54     ` Jeffrey R. Carter
2008-06-28  1:15       ` Adrian Hoe
2008-06-28  2:17         ` Adam Beneschan
2008-07-01 21:31           ` Randy Brukardt
2008-07-01 21:31           ` Randy Brukardt
2008-08-22  4:06           ` Adrian Hoe
2008-06-28  4:59         ` Jeffrey R. Carter
2008-06-29  3:48         ` anon
2008-06-28  1:21 ` anon
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox