C++代码中JAVA数据类型使用注意事项

17 篇文章 0 订阅
14 篇文章 0 订阅

一、javah头文件

    Java通过JNI机制调用c/c++写的native程序。c/c++开发的native程序需要遵循一定的JNI规范,下面的例子就是一个JNI函数声明:

JNIEXPORT jint JNICALL Java_jnitest_MyTest_test
  (JNIEnv * env, jobject obj, jint arg0);

    JVM负责从Java Stack转入C/C++ Native Stack。当Java进入JNI调用,除了函数本身的参数(arg0),会多出两个参数:JNIEnv指针和jobject指针。JNIEnv指针是JVM创建的,用于Native的c/c++方法操纵Java执行栈中的数据,比如Java Class, Java Method等。


二、Jni数据类型

    数据类型如下,记得在Get***之后都要调用Release***。否则会导致内存泄露。

Native
Code Type
Functions used
jbooleanNewBooleanArray
 GetBooleanArrayElements
 GetBooleanArrayRegion/SetBooleanArrayRegion
 ReleaseBooleanArrayElements
jbyteNewByteArray
 GetByteArrayElements
 GetByteArrayRegion/SetByteArrayRegion
 ReleaseByteArrayElements
jcharNewCharArray
 GetCharArrayElements
 GetCharArrayRegion/SetCharArrayRegion
 ReleaseCharArrayElements
jdoubleNewDoubleArray
 GetDoubleArrayElements
 GetDoubleArrayRegion/SetDoubleArrayRegion
 ReleaseDoubleArrayElements
jfloatNewFloatArray
 GetFloatArrayElements
 GetFloatArrayRegion/SetFloatArrayRegion
 ReleaseFloatArrayElements
jintNewIntArray
 GetIntArrayElements
 GetIntArrayRegion/SetIntArrayRegion
 ReleaseIntArrayElements
jlongNewLongArray
 GetLongArrayElements
 GetLongArrayRegion/SetLongArrayRegion
 ReleaseLongArrayElements
jobjectNewObjectArray
 GetObjectArrayElement/SetObjectArrayElement
jshortNewShortArray
 GetShortArrayElements
 GetShortArrayRegion/SetShortArrayRegion
 ReleaseShortArrayElements


三、jbyte使用注意

If you are using GetByteArrayElements you have to call ReleaseByteArrayElements after you are done with the array in JNI, because the JVM will prevent the freeing of this array in java until you do so.Please post the code to get a clearer idea.Something like:

boolean isCopy;
jbyte* b = GetByteArrayElements(env, arr, &isCopy);
You should be able to cast b to char* at this point in order to access the data in the array. Note that this may create a copy of the data, so you'll want to make sure to release the memory using ReleaseByteArrayElements:
ReleaseByteArrayElements(env, arr, b, 0);
The last parameter is a mode indicating how changes to b should be handled.

The last argument to the ReleaseByteArrayElements function above can have the following values:

    0: Updates to the array from within the C code are reflected in the Java language copy(indicates that the values are copied back to arr).
     
    JNI_COMMIT: The Java language copy is updated, but the local jbyteArray is not freed.

    JNI_ABORT: Changes are not copied back, but the jbyteArray is freed. The value is used only if the array is obtained with a get mode of JNI_TRUE meaning the array is a copy.(If you don't want to copy the data back to arr)

小心最后一个参数,如果为0是会释放 b 所指向的内存的. 如果b刚好指向一个栈上的数组的话,这样可能在Release 版本中造成内存方面的随机错误.可以用JNI_COMMIT来避免.


四、void *、long *问题

You can use jlong to pass a pointer (or a pointer to pointer, or whatever) back to Java. Java code won't be able to use it for anything, other than passing it as an argument to one of your other methods; but often that's all you really want.

If, on the other hand, you want Initialize() to be called with data set up in Java, then void * isn't appropriate; you'll need to use a Java class, and use reflection in JNI to get the information you need out of it.
Crudely, you could wrap malloc() and free():

jlong Java_c_utils_malloc(JNIEnv* env, jclass clazz, jint size) {
    return (jlong) malloc(size);
}

void Java_c_utils_free(JNIEnv* env, jclass clazz, jlong ptr) {
   free((void *) ptr);
}
and then use them (to no effect!) in Java:
long ptr = utils.malloc(100);
// Store ptr for a while
utils.free(ptr);

五、将C++中char *的buffer传递给Java

     char*如果是一般的字符串的话,作为string传回去就可以了。

    如果是含有’\0’的buffer,最好作为bytearray传出,因为可以制定copy的length,如果copy到string,可能到’\0’就截断了。有以下两种方式传递得到的数据:

    (1): 在jni中直接new一个byte数组,然后调用函数

(*env)->SetByteArrayRegion(env, bytearray, 0, len, buffer);
    将buffer的值copy到bytearray中,函数直接return bytearray。但是此方法容易产生内存泄露问题。

    (2):return错误号,数据作为参数传出。


参考文档:

http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html
http://docs.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 C++ 的 try-catch 语句时,需要注意以下几点: 1. try-catch 语句的语法:try 块包含可能会抛出异常的代码,catch 块包含处理异常的代码。catch 块可以有多个不同类型的异常处理程序。 2. try-catch 语句的执行顺序:当 try 块发生异常时,程序会跳转到与之匹配的 catch 块并执行对应的异常处理程序。如果没有匹配的 catch 块,则程序会终止。 3. 异常处理程序的顺序:在 catch 块定义多个异常处理程序时,这些处理程序的顺序很重要。如果一个异常可以匹配到多个处理程序,则会选择第一个匹配的处理程序。因此,应该按照从特殊到一般的顺序定义异常处理程序。 4. 异常的类型:在 try 块抛出的异常必须是可以被 catch 块的异常处理程序捕获的类型。如果抛出的异常类型不是 catch 块指定的类型,则该异常会继续向上层传递,直到被捕获或者程序终止。 5. 异常的捕获范围:try-catch 块的作用域只在其内部,因此异常只能在其内部被捕获和处理。如果异常在 try 块外抛出,则无法被 try-catch 块捕获。 6. 异常的再次抛出:在 catch 块,可以使用 throw 语句重新抛出异常,以便在上层处理。如果需要重新抛出异常,应该在 catch 块的最后使用 throw 语句,并且不带任何参数。 以上是 C++ 使用 try-catch 语句时需要注意的几点。需要特别注意的是,try-catch 语句应该尽可能地用于处理预期的异常,不要用于处理常规的程序逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值