[foreign] Pointer to fixed-size array: is jexctract's mapping to Java correct?

Lev Serebryakov lev at serebryakov.spb.ru
Wed Feb 27 16:14:55 UTC 2019


On 27.02.2019 18:33, Maurizio Cimadamore wrote:

>>   But jextract maps it to `Pointer<Array<Double>>`. Is it correct?
>> Shouldn't it be `Pointer<Double>` nor `Array<Double>`?
> 
> Jextract seems correct here-  you have a double[2], which is translated
> as Array<Double> and that a pointer to that, which is
> Pointer<Array<Double>>.

> This all seems correct - of course in C you might not reason about
> things this way, but that's what the type info says - `out*` in your
> example expects a pointer to a region of memory that contains two
> contiguous doubles
 Which is indistinguishable from "pointer to double" for all practical
means in plain C... C compiler will not complain if pass pointer to one
double or pointer to first element of array with 2048 doubles (which is
more realisitc usage of this API). Typically there wiil be a pointer to
A LOT of doubles, not only 2, is expected, as you could imagine. It is
array-of-pairs-of-doubles which is not expressed by array of structs for
some reason (I don't know why).

> so Pointer<Array<Double>> seems correct, semantically.

 Hmm. It is correct from point of view of type system, but it is
incorrect from point of view of memory layout, when it is translated to
Java verbatim. "pointer to array" is something strange in plain C (see
comp.lang.c FAQ 6.12 about it: difference between array and pointer to
it is in type, but not in memory layout, as array is second-class
citizen in C). It is not pointer-to-pointer, it is equivalent (from
memory layout point of view) to array itself, and Pointer<Array<Double>>
looks like double-indirection (if we remember, that Array is fancy name
for Pointer in this case), which is not what this native API expected.

> This gives you what you want.
 Thank you!

-- 
// Black Lion AKA Lev Serebryakov



More information about the panama-dev mailing list