i have trying to pass array from c++ to java. no error in compilation. but failed to run (crash). when i debug the code, error occurs in return code
i try to implement this code how to return c++ char 2d array using JNI to JAVA
here is my code
JNIEXPORT jobjectArray JNICALL my_function();
// ...
// some code
// ...
// i want convert float point[3][2] to java
jclass intArray1DClass = env->FindClass("[I");
jclass intArray2DClass = env->FindClass("[[I");
// float point[3][2]
jint sizeX = 3;
jint sizeY = 2;
jobjectArray array2D = env->NewObjectArray(
sizeX, intArray2DClass, NULL);
for (jint i = 0; i < sizeX; i++)
{
jobjectArray array1D = env->NewObjectArray(
sizeY, intArray1DClass, NULL);
for (jint y = 0; y < sizeY; y++)
{
jfloatArray value = env->NewFloatArray(point[i][y]); // float point[3][2]
env->SetObjectArrayElement(array1D, y, value);
}
env->SetObjectArrayElement(array1D, i, array1D);
}
return array2D;
and this is how i call function from java. is it right?
float[][] point = my_function()
thank you
Update
Finally i use 1D array because its simple to write the code. and according to 1D or 2D array, what's faster? , we should use 1D array
jint sizeX = 6;
jfloatArray array1D = env->NewFloatArray(sizeX);
env->SetFloatArrayRegion(array1D, 0, sizeX, point);
return array1D;