tags:

views:

22

answers:

1

Hello,

I've tried looking at a bunch of forums, and despite a lot of tweaking and reanalyzing, I'm a little stuck.

I'm trying to return a jdoubleArray via the JNI from my C++ code, but the values I seem to be entering don't seem to be carrying through. Here's the code.

    jdoubleArray ret = env->NewDoubleArray(2);

jdouble* rotationsJ = (jdouble *) malloc(2*sizeof(jdouble));
rotationsJ[0] = 4; //whatever, some dummy values.
rotationsJ[1] = 4;

cout<<"Rotations 0..."<<rotationsJ[0]; //Java console tells me this is 4, as it should.
env->SetDoubleArrayRegion(ret, 0, 2, (const jdouble*)rotationsJ);

return  ret; 
//EDIT - my java code looks like the following 
    double[] rotations = new double[2];

    rotations = pnMain.rotateLeftRight(true); //PNMain is the calling class, rotateLeftRight returns a jDoubleArray. 
//Was wondering if my Java declaration of rotations is needed?

Now in the Java code, the returned array from the C code is zeroed out. I can't seem to change it's values. I would think using the SetDoubleArrayRegion function would (hopefully) make the array 'ret' have the contents of rotationsJ, but this isn't working. I'm simply getting an array of two elements whose values are both 0.0 being passed back to me in Java.

Any help/questions is/are appreciated.

EDIT - I can add some more info to this. I tried doing:

    jboolean isCopy; 
jdouble* test = env->GetDoubleArrayElements(ret, &isCopy); 
cout<<"Test ..."<<test[1]; //This actually printed out the second element's value in Java.

So the extra print statement I decided to try out - I copy over what I'm trying to return into another buffer in C, and just try printing it out. This actually reflects the value, while having been setup only using 'ret'. Could there be some problem in the return statement/in the Java code setup?

A: 

Your use of JNI looks right. You don't need the "new double[2]" in the Java code, since you immediately replace that value with the return value from rotateLeftRight.

This could happen if the "jdouble" assignments were being handled as integer types instead of double-precision float. For example, if you stored the 64-bit pattern for the integer 4 into a double, you'd see 0.0 when you printed it. Trying storing "4.0" instead of 4, or setting it to 4668012349850910720LL.

fadden
Tried that - the Java array returned is still zeroed out :(
sparkFinder