pigpig
asked on
JNI: modify array content in C++ code
Dear Experts,
A piece of my C code is as follows:
JNIEXPORT void JNICALL
Java_ABC_fillBuffer(JNIEnv *env, jobject obj, jintArray arr, jint size)
{
jboolean isCopy = JNI_FALSE;
jint *jarr = env->GetIntArrayElements(a rr, &isCopy);
for (int i=0; i<size; i++)
jarr[i] = i;
env->ReleaseIntArrayElemen ts(arr, jarr, JNI_ABORT);
}
When I passed a array to this function in Java code, I noticed that the contents in the array don't change.
What wrong is the code?
My Java code is:
int []arr = new int[40];
XXX.fillBuffer(arr, 40);
Thank you~~
pigpig
A piece of my C code is as follows:
JNIEXPORT void JNICALL
Java_ABC_fillBuffer(JNIEnv
{
jboolean isCopy = JNI_FALSE;
jint *jarr = env->GetIntArrayElements(a
for (int i=0; i<size; i++)
jarr[i] = i;
env->ReleaseIntArrayElemen
}
When I passed a array to this function in Java code, I noticed that the contents in the array don't change.
What wrong is the code?
My Java code is:
int []arr = new int[40];
XXX.fillBuffer(arr, 40);
Thank you~~
pigpig
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
wasn't that exactly what I said?
Sorry,pellep!
This site is not multithreading synchronized :-))))).
When I started writing the answer there are no comments there. He can decide about points!
This site is not multithreading synchronized :-))))).
When I started writing the answer there are no comments there. He can decide about points!
ASKER
Thanks to both pellep and msterjev. My problem is solved.
I really want to award you both but the system doesn't allow me to do so. Thank so much~~
I really want to award you both but the system doesn't allow me to do so. Thank so much~~
0-copy back the content and free the elems buffer
JNI_COMMIT-copy back the content but do not free the elems
buffer
JNI_ABORT- free the buffer without copying back the possible
changes
So the array must be released with:
env->ReleaseIntArrayElemen
So, use 0 instead of JNI_ABORT!