gpt4 book ai didi

java - JNI 在 C 中将 jint 转换为 native int

转载 作者:行者123 更新时间:2023-11-30 07:54:59 25 4
gpt4 key购买 nike

我在 FileOutputStream.java 中声明了以下函数

public native void write(int b) throws IOException;

我读过 this thread要将 jint 参数转换为 native int,您只需对其进行强制转换。我的 C 代码:

JNIEXPORT void JNICALL Java_FileOutputStream_write__I(JNIEnv* jni, jobject obj, jint b){
int native_b = (int)b;
printf(b);
}

如果我在 java 中调用该函数,我会收到以下错误消息:

#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc7e01f3b2, pid=8700, tid=0x00000000000020e4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_112-b15) (build 1.8.0_112-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [msvcrt.dll+0x4f3b2]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# <my_path>\JNI\hs_err_pid8700.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

所以我想我的 Actor 阵容是错误的。我需要做什么才能正确处理?

最佳答案

转换 jint 没有错,但是 printf 需要带参数的格式字符串。

您的代码尝试访问地址 b 处的 char *,但由于 b 不是实际地址,它会崩溃。查找编译器的 printf 文档(或任何 printf 文档,例如:https://linux.die.net/man/3/printf)。

关于java - JNI 在 C 中将 jint 转换为 native int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43537475/

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