第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 cgo 從 Go 返回 String[]

使用 cgo 從 Go 返回 String[]

Go
慕勒3428872 2023-08-14 16:51:06
我必須從 Java 調(diào)用 Go 函數(shù)。我正在使用cgo和JNA來執(zhí)行此操作。Go 例程所做的唯一事情就是分配內(nèi)存并返回一個char**.?從Java方面,我收到了文檔中提到的char**使用。String[]以下是 C 幫助程序和 Go 函數(shù)的詳細信息:static char** cmalloc(int size) {? ? return (char**) malloc(size * sizeof(char*));}static void setElement(char **a, char *s, int index) {? ? a[index] = s;}//export getSearchKeysAfunc getSearchKeysA() **C.char {? ? set_char := C.cmalloc(1)? ? defer C.free(unsafe.Pointer(set_char))? ? C.setElement(set_char, C.CString("hello world"), C.int(0))? ? return set_char}Java端:String[] getSearchKeysA();我收到的錯誤是:## A fatal error has been detected by the Java Runtime Environment:##? SIGSEGV (0xb) at pc=0x00007fff6b15323e, pid=92979, tid=0x0000000000000c07## JRE version: Java(TM) SE Runtime Environment (8.0_192-b12) (build 1.8.0_192-b12)# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.192-b12 mixed mode bsd-amd64 compressed oops)# Problematic frame:# C? [libsystem_kernel.dylib+0x723e]? __pthread_kill+0xa## Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again## An error report file with more information is saved as:# /Users/dfb3/datafabric/pocs/go-java-connector/hs_err_pid92979.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.#我注意到問題是在 malloc 分配內(nèi)存時出現(xiàn)的。我已經(jīng)嘗試過執(zhí)行并從方法中ulimit -c unlimited刪除。defer C.free(unsafe.Pointer(set_char))錯誤的原因可能是什么以及如何解決?還有其他方法可以[]string使用 Go 從 Go返回 a 嗎JNA?由于拼寫錯誤并基于 @PeterSO 答案進行更新:我最初寫的是malloc(0),但應(yīng)該是malloc(1)func C.CString(string) *C.char,它應(yīng)該為我分配內(nèi)存,不是嗎?
查看完整描述

2 回答

?
料青山看我應(yīng)如是

TA貢獻1772條經(jīng)驗 獲得超8個贊

我終于可以String[]從GO使用中返回 a 了cgo。


我將留下函數(shù)簽名:


//export getSearchKeys

func getSearchKeys(numKeysByReference *C.int) **C.char {

  *numKeysByReference = // ... some value

  // Using the C helper defined above

  set_char := C.cmalloc(*numKeysByReference)

  // Logic allocating and populating C.char[i .. *numKeysByReference]

  // ...

  return set_char

}

**C.char使用 創(chuàng)建結(jié)構(gòu)后cgo,Java我收到的數(shù)據(jù)如下:


IntByReference intByReference = new IntByReference();

PointerByReference array = lib.getSearchKeys(intByReference);

String[] results = array.getPointer().getStringArray(0, intByReference.getValue());

正如@PeterSO提到的,我們defer C.free()在使用它后進行了調(diào)用。否則,返回后將被釋放。


查看完整回答
反對 回復 2023-08-14
?
小唯快跑啊

TA貢獻1863條經(jīng)驗 獲得超2個贊

你寫:


func getSearchKeysA() **C.char {

? ? set_char := C.cmalloc(0)

? ? defer C.free(unsafe.Pointer(set_char))

? ? C.setElement(set_char, C.CString("hello world"), C.int(0))

? ? return set_char

}

它可能執(zhí)行為:


func getSearchKeysA() (retval **C.char) {

? ? set_char := C.cmalloc(42)

? ? C.setElement(set_char, C.CString("hello world"), C.int(1))

? ? retval = set_char

? ? C.free(unsafe.Pointer(set_char))

? ? return retval

}

你指的是set_char之后嗎free


Go 編程語言規(guī)范2019 年 7 月 31 日版本

推遲陳述

“defer”語句調(diào)用一個函數(shù),該函數(shù)的執(zhí)行被推遲到周圍函數(shù)返回的那一刻,要么是因為周圍函數(shù)執(zhí)行了 return 語句,到達了其函數(shù)體的末尾,要么是因為相應(yīng)的 goroutine 正在恐慌。


你寫:

set_char?:=?C.cmalloc(0)
static?char**?cmalloc(int?size)?{
????return?(char**)?malloc(size?*?sizeof(char*));
}

$ 人 malloc

malloc() 函數(shù)分配 size 字節(jié)并返回指向所分配內(nèi)存的指針。內(nèi)存未初始化。如果 size 為 0,則 malloc() 返回 NULL,或稍后可以成功傳遞給 free() 的唯一指針值。

為什么分配大小 0(零)?

malloc內(nèi)存未初始化。


查看完整回答
反對 回復 2023-08-14
  • 2 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號