亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

使用 cgo 從 Go 返回 String[]

使用 cgo 從 Go 返回 String[]

Go
慕勒3428872 2023-08-14 16:51:06
我必須從 Java 調用 Go 函數。我正在使用cgo和JNA來執行此操作。Go 例程所做的唯一事情就是分配內存并返回一個char**.?從Java方面,我收到了文檔中提到的char**使用。String[]以下是 C 幫助程序和 Go 函數的詳細信息: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 分配內存時出現的。我已經嘗試過執行并從方法中ulimit -c unlimited刪除。defer C.free(unsafe.Pointer(set_char))錯誤的原因可能是什么以及如何解決?還有其他方法可以[]string使用 Go 從 Go返回 a 嗎JNA?由于拼寫錯誤并基于 @PeterSO 答案進行更新:我最初寫的是malloc(0),但應該是malloc(1)func C.CString(string) *C.char,它應該為我分配內存,不是嗎?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

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


我將留下函數簽名:


//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使用 創建結構后cgo,Java我收到的數據如下:


IntByReference intByReference = new IntByReference();

PointerByReference array = lib.getSearchKeys(intByReference);

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

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


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

TA貢獻1863條經驗 獲得超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

}

它可能執行為:


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 編程語言規范2019 年 7 月 31 日版本

推遲陳述

“defer”語句調用一個函數,該函數的執行被推遲到周圍函數返回的那一刻,要么是因為周圍函數執行了 return 語句,到達了其函數體的末尾,要么是因為相應的 goroutine 正在恐慌。


你寫:

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

$ 人 malloc

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

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

malloc內存未初始化。


查看完整回答
反對 回復 2023-08-14
  • 2 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號