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

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

使用 cgo 從 Go 返回 String[]

使用 cgo 從 Go 返回 String[]

Go
蝴蝶不菲 2022-04-26 15:40:01
我必須從 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 中返回 a 使用JNA?由于錯字和基于@PeterSO 的答案而更新:我最初寫的是 malloc(0) 但它應該是 malloc(1)func C.CString(string) *C.char,它應該為我分配內存,不是嗎?
查看完整描述

2 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

我終于可以String[]從GOusing中返回 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結構后,我收到如下數據:cgoJava


IntByReference intByReference = new IntByReference();

PointerByReference array = lib.getSearchKeys(intByReference);

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

正如@PeterSO 提到的,我們defer C.free()在使用它后打電話。否則,它會在返回后被釋放。


查看完整回答
反對 回復 2022-04-26
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

你寫:


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嗎?


2019 年 7 月 31 日的 Go 編程語言規范版本


延遲語句


“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內存未初始化。


查看完整回答
反對 回復 2022-04-26
  • 2 回答
  • 0 關注
  • 367 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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