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: Thu, 26 Jun 2008 19:55:02 -0700 (PDT)
Date: 2008-06-26T19:55:02-07:00	[thread overview]
Message-ID: <e76b87f0-65bf-485c-943f-618389ab8a1b@w34g2000prm.googlegroups.com> (raw)
In-Reply-To: d390ae47-83b1-4cfb-8e66-d6a92f38cf4e@z32g2000prh.googlegroups.com

On Jun 26, 6:39 pm, Adrian Hoe <aby...@gmail.com> wrote:
> 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?

I've never heard of an Ada compiler that doesn't, absent some explicit
instruction (like a pragma) to do something unusual.  Even so, if
you're thinking of using an Ada compiler written on the planet
Zorxkrug where they might do some odd array allocation, you should
still be OK if you use the Convention pragma.  First of all, you
cannot declare PWM_Data with an anonymous array type, because then you
couldn't pass it as a parameter to *any* routine (what would the
parameter type be??).  So something like this should work:

    type Float_Array is array (Natural range <>) of Float;
    pragma Convention (C, Float_Array);
    PWM_Data : Float_Array (1 .. PWM_Count);

and now even the Zorxkrugian Ada compiler should allocate the array in
a way that will be compatible with the C code.  (In the Ada
declaration of pwmRead, the second parameter would have type
Float_Array.)

No guarantees, but a compiler that follows the Implementation Advice
should handle this correctly.

Oh, and you probably want to use Interfaces.C.C_Float instead of the
Ada type "Float", to ensure that you're using the same kind of float
that the C routine expects.

Hope this helps,

                            -- Adam



  parent reply	other threads:[~2008-06-27  2:55 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 [this message]
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
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