Re: Ada array vs C pointer (call by reference)
- From: Adam Beneschan <adam@xxxxxxxxxx>
- Date: Thu, 26 Jun 2008 19:55:02 -0700 (PDT)
On Jun 26, 6:39 pm, Adrian Hoe <aby...@xxxxxxxxx> 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
.
- Follow-Ups:
- Re: Ada array vs C pointer (call by reference)
- From: Maciej Sobczak
- Re: Ada array vs C pointer (call by reference)
- References:
- Ada array vs C pointer (call by reference)
- From: Adrian Hoe
- Ada array vs C pointer (call by reference)
- Prev by Date: Re: Out parameters and unconstrained variant types
- Next by Date: Re: Ada array vs C pointer (call by reference)
- Previous by thread: Re: Ada array vs C pointer (call by reference)
- Next by thread: Re: Ada array vs C pointer (call by reference)
- Index(es):
Relevant Pages
|
Loading