Java JNI将jbyteray转换为std::string 我已经使用和适应C++:如下:< /P> jsize num_bytes = env->GetArrayLength(message); char *buffer = new char(num_bytes + 1); if (!buffer) { // handle allocation failure ... //throw error? return 1; } // obtain the array elements jbyte* elements = env->GetByteArrayElements(message, NULL); if (!elements) { // handle JNI error ... //throw error? return 2; } // copy the array elements into the buffer, and append a terminator memcpy(buffer, elements, num_bytes); buffer[num_bytes] = 0; std::string m(buffer, num_bytes + 1); // Do not forget to release the element array provided by JNI: env->ReleaseByteArrayElements(message, elements, JNI_ABORT);

Java JNI将jbyteray转换为std::string 我已经使用和适应C++:如下:< /P> jsize num_bytes = env->GetArrayLength(message); char *buffer = new char(num_bytes + 1); if (!buffer) { // handle allocation failure ... //throw error? return 1; } // obtain the array elements jbyte* elements = env->GetByteArrayElements(message, NULL); if (!elements) { // handle JNI error ... //throw error? return 2; } // copy the array elements into the buffer, and append a terminator memcpy(buffer, elements, num_bytes); buffer[num_bytes] = 0; std::string m(buffer, num_bytes + 1); // Do not forget to release the element array provided by JNI: env->ReleaseByteArrayElements(message, elements, JNI_ABORT);,java,android,c++,Java,Android,C++,我必须改变char*buffer=malloc(num_bytes+1)tochar*buffer=新字符(num_字节+1)因为C++错误。为什么它不能在C++中通过工作方式?< /P> 好吧,这段代码让我崩溃了。我认为这可能与我所做的更改有关,因为env->ReleaseByteArrayElements(消息、元素、JNI\u中止)可能会释放它,就好像它是由malloc分配的一样 < C++中的代码应该如何工作?专门将 MaloC < /C> > char */COD>: char *b

我必须改变char*buffer=malloc(num_bytes+1)to
char*buffer=新字符(num_字节+1)因为C++错误。为什么它不能在C++中通过工作方式?< /P>
好吧,这段代码让我崩溃了。我认为这可能与我所做的更改有关,因为
env->ReleaseByteArrayElements(消息、元素、JNI\u中止)
可能会释放它,就好像它是由
malloc
分配的一样


< C++中的代码应该如何工作?

专门将<代码> MaloC < /C> > <代码> char */COD>:

char *buffer = (char *) malloc(num_bytes + 1);

您的链接示例使用了
C
,这就是造成这里细微差别的原因

C++
编译器假定
malloc()
返回一个
int
,从而导致目标指针类型的类型大小不一致(
char注意您选择的括号(圆形与方形)

char*buffer=新字符(num_字节+1);
…创建一个值为
num_bytes+1
char

char*buffer=新字符[num_字节+1];
…创建一个包含
num_bytes+1
number of
char
s的数组。应该就是这样


相关的:

  • new char(N)
    分配一个值为
    N
    char
    。如果需要一个
    N
    字符数组,请改用
    new char[N]

    也就是说,您实际上根本不需要
    char[]
    数组。您可以按原样使用Java字节数组元素构造
    std::string
    ,例如:

    jsizenum\u bytes=env->GetArrayLength(消息);
    //获取数组元素
    jbyte*elements=env->GetByteArrayElements(消息,NULL);
    如果(!元素){
    //处理JNI错误。。。
    //抛出错误?
    返回1;
    }
    //将数组元素复制到字符串中
    std::字符串m(重新解释转换(元素),num_字节);
    //不要忘记释放JNI提供的元素数组
    环境->ReleaseByteArrayElements(消息、元素、JNI\u中止);
    
    或者,至少可以预先设定
    std::string
    的大小,然后将字节数组元素复制到字符串自己的内存缓冲区中,例如:

    jsizenum\u bytes=env->GetArrayLength(消息);
    //获取数组元素
    jbyte*elements=env->GetByteArrayElements(消息,NULL);
    如果(!元素){
    //处理JNI错误。。。
    //抛出错误?
    返回1;
    }
    //将数组元素复制到字符串中
    std::字符串m(num_字节,'\0');
    std::copy_n(元素,num_字节,m.begin());
    //不要忘记释放JNI提供的元素数组
    环境->ReleaseByteArrayElements(消息、元素、JNI\u中止);