[foreign] Pointer to fixed-size array: is jexctract's mapping to Java correct?
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Feb 27 17:49:56 UTC 2019
On 27/02/2019 16:14, Lev Serebryakov wrote:
> 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.
Not sure I follow. A pointer to an array has a very different layout
from an array.
#include <stdio.h>
typedef int ARR[5];
int main(void) {
printf("sizeof ARR = %d\n", sizeof(ARR));
printf("sizeof ARR* = %d\n", sizeof(ARR*));
return 0;
}
this prints:
sizeof ARR = 20
sizeof ARR* = 8
As expected.
What I think you are saying is, I believe, that if you have a
Pointer<Array<X>> you can always flatten it as a Pointer<X>; while this
is technically correct (the underlying layout is correct), when you do
the flattening you completely lose track of the fact that the memory
region is not just a sequence of X - but a sequence _of sequence_ of X.
In your example, I agree that you might want to pass an array of 2048
double to the function and be happy with it. But, what if you pass an
array with only _one_ ? Or an array with an _odd_ number of doubles?
This is allowed if we're just talking about a flattened double* - but,
semantically speaking, this is NOT what the code says with the typedef.
I tried to write examples along these lines:
typedef double fftw_complex[2];
int main(void) {
double arr[100];
fftw_complex *arg = arr;
return 0;
}
And I always get some warnings when trying to pass a flattened double
array as a pointer to double[].
The code might work at runtime, assuming you are not misusing the API
(see above, e.g. by passing an odd number of doubles), but this is still
a potentially unsound operation.
Maurizio
More information about the panama-dev
mailing list