0

I have an ArrayList of float arrays as ArrayList<float[]> which I want to map to C++ Vector<array<float,size>> in JniWrapper.

I followed this link:

"Returning an arraylist of string from Native java to JNI"

and made few changes to the code as per my requirement.

static jclass java_util_ArrayList;
static jmethodID java_util_ArrayList_;
jmethodID java_util_ArrayList_size;
jmethodID java_util_ArrayList_get;
jmethodID java_util_ArrayList_add;
static thread_local JNIEnv *env;

void java2cpp(jobject arrayList, vector<array<float, 320>> &result) {
    java_util_ArrayList = static_cast<jclass>(env->NewGlobalRef(
            env->FindClass("java/util/ArrayList")));
    java_util_ArrayList_ = env->GetMethodID(java_util_ArrayList, "init", "(I)V");
    java_util_ArrayList_size = env->GetMethodID(java_util_ArrayList, "size", "()I");
    java_util_ArrayList_get = env->GetMethodID(java_util_ArrayList, "get", "(I)Ljava/lang/Object;");
    java_util_ArrayList_add = env->GetMethodID(java_util_ArrayList, "add", "(Ljava/lang/Object;)V");


    jint len = env->CallIntMethod(arrayList, java_util_ArrayList_size);
    result.reserve(len);
    for (jint i = 0; i < len; i++) {
        jfloatArray element = static_cast<jfloatArray>(env->CallObjectMethod(arrayList,
                                                                             java_util_ArrayList_get,
                                                                             i));
        const float *pchars = env->GetFloatArrayElements(element, nullptr);
        result.emplace_back(pchars);
        env->ReleaseFloatArrayElements(element, const_cast<jfloat *>(pchars), 0);
        env->DeleteLocalRef(element);
    }
}

jfloatArray cpp2java(array<float, 320> output) {
    jfloatArray result;
    result = env->NewFloatArray(320);

    float *data;
    data = output.data();

    env->SetFloatArrayRegion(result, 0, 320, data);
    free(data);
    return result;

}

Error:

note: in instantiation of function template specialization 'std::__ndk1::vector<std::__ndk1::array<float, 320>, std::__ndk1::allocator<std::__ndk1::array<float, 320> > >::emplace_back<const float *&>' requested here
        result.emplace_back(pchars);
           ^

I am new to the development of JNI Wrappers. I need help in mapping ArrayList<float[]> to Vector<array<float,320>>.

2
  • 1
    Your vector holds array<float, 320> but you are passing it a const float *. You need to create the array of the correct type and fill it with appropriate data. Commented Oct 18, 2019 at 16:25
  • Hi @ChrisMM, could you please explain a little bit more? Commented Oct 18, 2019 at 16:28

2 Answers 2

0

the type of result is: vector<array<float, 320>> which is passed by reference. However you emplace_back a const float*:

const float *pchars = env->GetFloatArrayElements(element, nullptr);
 result.emplace_back(pchars);

You can solve the problem by creating an array out of the data pointed by pchar e.g.

std::array<float, 320> res;
for (int i = 0; i < end; ++i)
     res[i] = pchars[i];
result.emplace_back(res);

Make sure to range check pchar (end) you may access out of bounds.

Sign up to request clarification or add additional context in comments.

4 Comments

So what should I do..??
Okay Thanks alot. Let me try the solution.
@KhubaibAhmad "My problem has been solved" - Then you should Accept the answer that solved the problem, rather than leaving a "thank you" comment.
@JesperJuhl My apologies.. there were some technical issues and I was unable to do it.
0

Your vector holds array<float, 320> but you are passing it a const float *. You need to create the array of the correct type and fill it with appropriate data. At no point do you create the array<float, 320>, nor do you fill it. It's been a while since I've used JNI so I don't really remember the syntax, but you would have to copy the elements in the jfloatarray over into a new array<float, 320>.

Assuming pchars contains the appropriate data, then you should be able to do something like:

const float *pchars = env->GetFloatArrayElements(element, nullptr);
array<float, 320> newArray;
for ( size_t i = 0; i < 320; ++i ) newArray[i] = pchars[i];
result.emplace_back(newArray);

1 Comment

Thanks alot @ChrisMM. I think this is the right solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.