从**字符到蟒蛇列表

我已经解决了这个问题,请参阅问题下面的答案。


但我最终发现将GO函数嵌入到Python中是非常愚蠢的。这种嵌入失败主要是因为Go函数几乎不知道何时回收内存资源,从而导致内存泄漏。


目前,我意识到将它们组合在一起的最好方法可能是信息交流,就像袜子一样。


如果我的想法是错误的,请告诉我任何正确的事情。


原始问题:

在 C 端,函数返回一个字符串数组(例如 [“i 0”、“i 1”、“i 2”、“i 3”]),类型为 。**char


在 Python 端,该输出被读入一个变量(比如说),其类型为**charcArrayPOINTER(c_char_p)


我的问题:如何创建一个python列表?即获得cArraypylist == ["i 0","i 1","i 2","i 3"]


我还想知道在python中是否有一个值获取操作,就像C中的*操作一样。


下面是代码示例:


C面(实际去)

package main


//#include <stdlib.h>

import "C"

import (

    "unsafe"

)


//export TestLoad

func TestLoad(cstr *C.char) **C.char {

    gostr := C.GoString(cstr)

    goslice := []string{gostr, "i 0", "i 1", "i 2", "i 3"}

    cArray := C.malloc(C.size_t(len(goslice)) * C.size_t(unsafe.Sizeof(uintptr(0))))

    defer C.free(unsafe.Pointer(cArray))

    temp := (*[1<<30 - 1]*C.char)(cArray)

    for k, v := range goslice {

        temp[k] = C.CString(v)

    }


    return (**C.char)(cArray)

}


func main() {

}

蟒蛇侧

from ctypes import *


mylib = cdll.LoadLibrary("./mylib.so")

mylib.TestLoad.argtype = c_char_p

mylib.TestLoad.restype = POINTER(c_char_p)  # ***Is it possible to have two or more restypes?*** 


pystr = "hello"                             #  python str

b = pystr.encode("utf-8")                   #  convert str to bytes

resp = mylib.TestLoad(b)                    #  call CGo function, and get resp typed POINTER(c_char_p)

list_len = 5                                #  assume the length of list is known

'''

TODO

'''

顺便说一句,单个C或CGO函数是否有可能具有两个或多个返回?我尝试过,但未能成功。


感谢您的帮助。


幕布斯6054654
浏览 79回答 1
1回答

缥缈止盈

溶液:C 侧 (GO) -- 应注释位置的行defer C.free...package main//#include <stdlib.h>import "C"import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unsafe")//export TestLoadfunc TestLoad(cstr *C.char) **C.char {&nbsp; &nbsp; gostr := C.GoString(cstr)&nbsp; &nbsp; fmt.Println("In Go: ", gostr)&nbsp; &nbsp; goslice := []string{gostr, "i 0", "i 1", "i 2", "i 3"}&nbsp; &nbsp; cArray := C.malloc(C.size_t(len(goslice)) * C.size_t(unsafe.Sizeof(uintptr(0))))&nbsp; &nbsp; // defer C.free(unsafe.Pointer(cArray))&nbsp; &nbsp; temp := (*[1<<30 - 1]*C.char)(cArray)&nbsp; &nbsp; for k, v := range goslice {&nbsp; &nbsp; &nbsp; &nbsp; temp[k] = C.CString(v)&nbsp; &nbsp; }&nbsp; &nbsp; return (**C.char)(cArray)}func main() {}蟒蛇侧 -- 修改:指针(c_char_p * 5),然后调用 resp.content 来访问每个蟒蛇字节。有关源代码,请参阅示例 #17。from ctypes import *mylib = cdll.LoadLibrary("./_mylib.so")mylib.TestLoad.argtype = c_char_pmylib.TestLoad.restype = POINTER(c_char_p*5)&nbsp; &nbsp; &nbsp;&nbsp;pystr = "Hello, world!"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b = pystr.encode("utf-8")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;resp = mylib.TestLoad(b)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;'''**********************************'''output = []for seq in resp.contents:&nbsp; &nbsp; s = seq.decode("utf-8")&nbsp; &nbsp; output.append(s)&nbsp; &nbsp; print(s,type(s))print(output)'''**********************************'''一个新的问题出现了:会评论 // 推迟 C.免费(不安全。指针(cArray))导致C端的内存泄漏在这个例子中?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go