简体   繁体   English

jboolean* 和 uint64_t* 之间的转换

[英]Conversion between jboolean* and uint64_t*

I'm trying to convert a jbooleanArray with 128 elements (always) to a C++ array of bool s with 128 elements also.我正在尝试将具有 128 个元素(始终)的jbooleanArray转换为也具有 128 个元素的bool的 C++ 数组。

extern "C" {
JNIEXPORT jboolean  Java_com_app_flutter_1app_JNI_loadBufferNative(
        JNIEnv *env, jbooleanArray jMidiNotes) {
    bool midiNotes[128] = {false};
    *reinterpret_cast<uint64_t*>(midiNotes) = *env->GetBooleanArrayElements(jMidiNotes, nullptr);
    *reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 64, nullptr);

I believe GetBooleanArrayElements returns jboolean* , and it looks like a jboolean is uint8_t in C++ (strange).我相信GetBooleanArrayElements返回jboolean* ,看起来jboolean是 C++ 中的uint8_t (奇怪)。

What am I doing wrong here?我在这里做错了什么? I get a crash JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x7ff426b390 .我收到崩溃JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x7ff426b390

Since jboolean = uint8_t I also tried由于jboolean = uint8_t我也试过

    *reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 8, nullptr);

but I get the same crash但我遇到了同样的崩溃

You can't do pointer arithmetic on object handles like that:您不能像这样对 object 句柄进行指针运算:

*reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 64, nullptr);

You should first get a pointer to the array elements using GetBooleanArrayElements and then do pointer arithmetic on that pointer.您应该首先使用GetBooleanArrayElements获取指向数组元素的指针,然后对该指针进行指针运算。 For instance, do like this:例如,这样做:

extern "C" {
JNIEXPORT jboolean  Java_com_app_flutter_1app_JNI_loadBufferNative(
    JNIEnv *env, jbooleanArray jMidiNotes) {
    bool midiNotes[128] = {false};
    jboolean* values = env->GetBooleanArrayElements(jMidiNotes, nullptr);
    std::copy(values, values + 128, midiNotes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM