Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JNI: modify array content in C++ code

Posted on 2002-05-29
5
Medium Priority
?
3,398 Views
Last Modified: 2013-11-23
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(arr, &isCopy);
for (int i=0; i<size; i++)
   jarr[i] = i;
env->ReleaseIntArrayElements(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
0
Comment
Question by:pigpig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 200 total points
ID: 7041586
Try
env->ReleaseIntArrayElements(arr, jarr, 0);
instead of
env->ReleaseIntArrayElements(arr, jarr, JNI_ABORT);
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7041615
The JNI specification says:

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->ReleaseIntArrayElements(arr, jarr, 0);

So, use 0 instead of JNI_ABORT!
0
 
LVL 4

Expert Comment

by:pellep
ID: 7041644
wasn't that exactly what I said?
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7042975
Sorry,pellep!
This site is not multithreading synchronized :-))))).
When I started writing the answer there are no comments there. He can decide about points!
0
 

Author Comment

by:pigpig
ID: 7043563
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~~
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question