gpt4 book ai didi

Android NDK jshortArray 没有宣传的那么长

转载 作者:行者123 更新时间:2023-11-30 04:09:04 26 4
gpt4 key购买 nike

我遇到以下代码的段错误:

#include <stddef.h>
#include <android/log.h>
#include "com_backyardbrains_drawing_BybGLDrawable.h"

#define ALOG(msg) __android_log_print(ANDROID_LOG_DEBUG, "NDK:BYB", msg)
#define AERR(msg) __android_log_print(ANDROID_LOG_ERROR, "NDK:BYB", msg)

JNIEXPORT jfloatArray JNICALL
Java_com_backyardbrains_drawing_BybGLDrawable_interleaveXvalues
(JNIEnv *env, jclass clazz, jshortArray shortArrayToDraw) {
ALOG("called interleave");

jfloatArray result;
jsize len = (*env)->GetArrayLength(env, shortArrayToDraw);
__android_log_print(ANDROID_LOG_DEBUG, "NDK:BYB",
"Got input length of %d", len);
jshort *valuesjs = (*env)->GetShortArrayElements(env, shortArrayToDraw, NULL);
if (valuesjs == NULL || !(len > 0)) {
return NULL;
}
jfloat tmp[len*2];
__android_log_print(ANDROID_LOG_DEBUG, "NDK:BYB",
"made a new tmp array of size %d",
sizeof(tmp)/sizeof(jfloat));
// Create the returnable 2D array
result = (*env)->NewFloatArray(env, len*2);
if (result == NULL) {
AERR("could not allocate result");
return NULL;
}

int i;
for (i = 0; i < len; i++) {
tmp[i*2] = i;
tmp[i*2+1] = valuesjs[i];
__android_log_print(ANDROID_LOG_DEBUG, "NDK:BYB", "Set element %d", i);
}
ALOG("Finished building new array");

(*env)->ReleaseShortArrayElements(env, shortArrayToDraw, valuesjs, 0);
ALOG("released old short array");

(*env)->SetFloatArrayRegion(env, result, 0, len*2, tmp);
ALOG("set new float array");
return result;

}

日志输出是这样的:

06-19 21:34:24.829 29156 29172 D NDK:BYB : called interleave
06-19 21:34:24.829 29156 29172 D NDK:BYB : Got input length of 131072
06-19 21:34:24.829 29156 29172 D NDK:BYB : made a new tmp array of size 262144
06-19 21:34:25.157 29156 29172 D NDK:BYB : Set element 0
...
06-19 21:34:25.157 29156 29172 D NDK:BYB : Set element 158
06-19 21:34:25.367 29204 29204 I DEBUG : pid: 29156, tid: 29172 >>> com.backyardbrains <<<
06-19 21:34:25.367 29204 29204 I DEBUG : signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 450ac000

对于 for 循环,问题始终出现在 159 for i 处。但是,传入此函数的数组的预期长度是固定的 131072 个元素(如日志中所示)。为什么数组下方的值无法访问?

最佳答案

也许您的环境无法很好地处理在堆栈上分配的如此多的内存。因为 sizeof(tmp) == 1 MB。尝试在堆上分配 tmp。

关于Android NDK jshortArray 没有宣传的那么长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11111869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com