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

本文详细介绍了Java Native Interface (JNI) 的核心概念,包括如何通过JNI机制实现Java与C/C++之间的互操作,解释了JNI头文件的构成及JNI数据类型的转换方法,并提供了具体的使用注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值