Issue
I'm studying the C usage in Android App through NDK. In my first attempt, I would like to pass a java int array as an argument in a C function. The project compile, I don't have any compilation problem.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Sample s=new Sample();
int[] b={1, 2, 3};
int a=s.sum(b);
Log.i("aa","sum= "+a);
}
public class Sample {
static {
System.loadLibrary("sample");
Log.i("a", "Load sample");
}
public native int somma(int[] b);
}
#include <jni.h>
#include "sample.h" // Generated
JNIEXPORT jint JNICALL
Java_com_example_myapplication_Sample_somma(JNIEnv *env, jobject instance, jintArray b_) {
jint *b = (*env)->GetIntArrayElements(env, b_, NULL);
size_t n = sizeof(b_) / sizeof(jint);
jint result=0;
for (int i=0;i<n;i++) {
result+=(*b);
b++;
}
(*env)->ReleaseIntArrayElements(env, b_, b, 0);
return result;
}
The problem is on size_t n = sizeof(b_) / sizeof(jint);
instruction. How can I determine the number of elements of array b_?
Thank you in advance.
Solution
You can obtain the length of your array with GetArrayLength
:
jsize arrayLength = (*env)->GetArrayLength(env, b_);
Answered By - MrPromethee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.