comp.lang.ada
 help / color / mirror / Atom feed
From: Adrian Hoe <abyhoe@gmail.com>
Subject: Re: Ada array vs C pointer (call by reference)
Date: Fri, 27 Jun 2008 01:22:01 -0700 (PDT)
Date: 2008-06-27T01:22:01-07:00	[thread overview]
Message-ID: <dae8b954-fda8-42f8-8953-886896a7776e@u6g2000prc.googlegroups.com> (raw)
In-Reply-To: kLZ8k.227466$yE1.93121@attbi_s21

On Jun 27, 12:10 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> 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



  reply	other threads:[~2008-06-27  8:22 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 [this message]
2008-06-27 15:07     ` Adam Beneschan
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