jboolean* 和 uint64_t* 之间的转换
Conversion between jboolean* and uint64_t*
我正在尝试将具有 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);
我相信 GetBooleanArrayElements
returns jboolean*
,看起来 jboolean
在 C++ 中是 uint8_t
(奇怪)。
我在这里做错了什么?我崩溃了 JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x7ff426b390
。
因为jboolean = uint8_t
我也试过
*reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 8, nullptr);
但我遇到了同样的崩溃
你不能像这样对对象句柄进行指针运算:
*reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 64, nullptr);
您应该首先使用 GetBooleanArrayElements
获取指向数组元素的指针,然后对该指针进行指针运算。例如,这样做:
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);
我正在尝试将具有 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);
我相信 GetBooleanArrayElements
returns jboolean*
,看起来 jboolean
在 C++ 中是 uint8_t
(奇怪)。
我在这里做错了什么?我崩溃了 JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x7ff426b390
。
因为jboolean = uint8_t
我也试过
*reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 8, nullptr);
但我遇到了同样的崩溃
你不能像这样对对象句柄进行指针运算:
*reinterpret_cast<uint64_t*>(midiNotes + 64) = *env->GetBooleanArrayElements(jMidiNotes + 64, nullptr);
您应该首先使用 GetBooleanArrayElements
获取指向数组元素的指针,然后对该指针进行指针运算。例如,这样做:
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);