dougou6727 2018-08-17 08:11
浏览 189
已采纳

指针字符串切片(* [] string)的Cgo指针传递规则?

Can I pass *[]string to C from Go and then append to the string slice, or is it violating the pointer passing spec?

Go code may pass a Go pointer to C, provided the Go memory to which it points does not contain any Go pointers.

Example code:

package main

/*
extern void go_callback(void*, char*);

static inline void callback(void* stringSliceGoPointer) {
    go_callback(stringSliceGoPointer, "foobar");
}
*/
import "C"

import (
    "fmt"
    "unsafe"
)

func main() {
    a := make([]string, 0)
    C.callback(unsafe.Pointer(&a)) 
    fmt.Println(a[0]) // outputs foobar
}

//export go_callback
func go_callback(stringSliceGoPointer unsafe.Pointer, msg *C.char) {
    slice := (*[]string)(stringSliceGoPointer)
    *slice = append(*slice, C.GoString(msg))
}
  • 写回答

2条回答 默认 最新

  • dtf579777 2018-08-23 18:40
    关注

    Passing a *[]string to C is not allowed because the memory pointed to contains strings and strings contain pointers. As the cgo docs say (emphasis mine)

    Note that values of some Go types, other than the type's zero value, always include Go pointers. This is true of string, slice, interface, channel, map, and function types.

    One way to overcome this is to refer to the []string more indirectly so only Go code actually knows its address. For example:

    package main
    
    /*
    extern void go_callback(int, char*);
    
    static inline void callback(int stringSliceRef) {
        go_callback(stringSliceRef, "foobar");
    }
    */
    import "C"
    
    import (
        "fmt"
    )
    
    // If you need to use these values concurrently,
    // you'll need more code to protect this.
    var stringSlices = make([][]string, 10)
    
    func main() {
        C.callback(0) 
        fmt.Println(stringSlices[0][0]) // outputs foobar
    }
    
    //export go_callback
    func go_callback(ref C.int, msg *C.char) {
        i := int(ref)
        stringSlices[i] = append(stringSlices[i], C.GoString(msg))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图