我有一个大问题,我没有用过 C++。我正在通过 jni 将一个数组从 java 传递到 c++,我想在 c++ 中创建一个相同长度的新数组。以下是代码:
#include <string.h>
#include <jni.h>
extern "C"
{
JNIEXPORT void JNICALL Java_com_profesionphotostudio_pps_Imageprocessing_process(JNIEnv* env, jobject thiz, jintArray pixelArray, jintArray ddimensions, jint function)
{
int *pixels = env->GetIntArrayElements( pixelArray, 0);
jsize length = env->GetArrayLength(pixelArray);
int *d= env->GetIntArrayElements(ddimensions, 0);
int i, j, pixel, red, green, blue, h,w;
w=d[0]; h =d[1];
int *temppixel= new int[length];
for(i=0; i<=w*h; i++)
{
temppixel[i]=-1;
}
delete [] temppixel;
env->ReleaseIntArrayElements( pixelArray, pixels, 0);
env->ReleaseIntArrayElements(ddimensions, d, 0);
}
但是我收到这个错误:
Fatal signal 11 (SIGSEGV) at 0x00097756 (code=1), thread 9082 (photostudio.pps)
当我手动设置时
length = 100000;
我的代码工作得很好。我也试过设置
length = w*h;
但我收到同样的致命信号错误
在 ddimensions 数组中,我保留位图的高度和宽度,pixelArray 包含该位图的 int 值
最佳答案
替换
for(i=0; i<=w*h; i++)
通过
for(i=0; i<w*h; i++)
或者您将访问超出其范围的数组(数组索引从 0
到 n-1
)。
代码有效,无论 w
和 h
的值是多少(至少是正值):
int h = 10;
int w = 15;
int *temppixel = new int[w*h];
for(i=0; i<w*h; i++)
{
temppixel[i] = -1;
}
delete [] temppixel;
这在 C++ 中绝对有效。如果它仍然崩溃,则问题一定来自其他地方(然后您需要隔离问题)。
关于android - c++ android ndk 使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469488/