Home > Mobile >  JNI array scheduling problems
JNI array scheduling problems

Time:10-02

//the JNI
Public native int arrays (int [] arr);

The inside of the//C file method
JNIEXPORT jint JNICALL Java_com_example_du_1dezheng_ndk_MainActivity_arrays
Env (JNIEnv *, jobject obj, jintArray jarr) {

Jsize len=(* env) - & gt; GetArrayLength (env, jarr);
Jint * cint=(* env) - & gt; GetIntArrayElements (env, jarr, NULL);
Int I=0;
int j=0;
Int temp=0;

for(i=0; I & lt; Len - 1; I++) {
For (j=0; J & lt; Len - I - 1; J++) {
If (cint [j] & gt; Cint [m + 1]) {
Temp=cint [j];
Cint [j]=cint [j + 1);
Cint [j + 1)=temp;
}

}
return 0;
}


Int [] arr=new int [],1,7,0,1,7,2,6 {3};
Int n=arrays (arr);

Finally can normal returns 0, but the array print it out, but have not sort to, or the original order,
Theory is the pointer to the array address, put the return for the inside can also re out, but no change to the array element position,
Did anyone met this case,,,

CodePudding user response:

In the final and this is normal,
(* env) - & gt; ReleaseIntArrayElements (env, jarr, cint, 0);


Strange ah, murphy pointer is not directly send it to address an array, but point to a copy?

Using the NDK r12b, even if the version difference should also won't difference so far?

Have a great god met before, explain the reason for this?

CodePudding user response:

Lz, share my last added env - & gt; ReleaseIntArrayElements (ja, p, 0);

CodePudding user response:

The following as ReleaseIntArrayElements function prototype
Void (* ReleaseIntArrayElements) (JNIEnv *, jintArray, jint, jint);
The function and GetIntArrayElements function can be said to be the corresponding,
It is the function of complete release resources and data updates,
Because the Java garbage collection has may change the location of the object in the memory, if you don't take the necessary measures,
Could no longer accessed array pointer pointing to the correct storage area, therefore, for an array, or "nail" it in a fixed area,
Or copy it to a fixed deposit area, in a word during a visit to it to make the array elements in place,
After finished the operation, and then calls the function, remove its fixed,
In addition, before calling this function, all updates have no effect on the array itself,
The third parameter is decided to update or not,
Value of zero (0), update the array and release all elements;
JNI_COMMIT values, update but not release all elements;
JNI_ABORT values, update but not release all elements;
  • Related