开发者

setting the value of a given cell in a jintArray

I get the arr int[] from the JVM and want to set a value in it at 开发者_开发百科a specified index, like this:

jintArray arr;
jint* ints = _env->GetIntArrayElements(arr, false);
int newvalue = 4;

_env->SetIntArrayRegion(ints, 3, 1, &newvalue); // this works
inst[3] = newvalue; // this failed !!!

Can you tell me why the second assignment fails??? It should work and be much faster (no method call).

Thanks, Luc


As Luc mentioned, he is talking about JNI. The general answer is that the JVM's GC may move around the backing store to an array. The type jintArray is just a typedef which really only indicates a handle to an array.

If you are manipulating the array, you must either use SetIntArrayRegion, which copies the passed pointer into the backing store,

OR you can use a combination of GetIntArrayElements, which will pin or copy the array, followed by ReleaseIntArrayElements.

Since jintArray is not a proper C array, you cannot use the indexer operator[].


JIntArray is itself not a primitive array, it just contains a primitive array. To set values, you can use the set(int index, int value) method (see API for more details), or you can get the array using the JIntArray toArray() method, and use that array instead.

In your context, the first method would look like this:

arr.set(3, newvalue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜