douzhu5900 2018-06-28 22:18
浏览 135

用uint8_t * num将C结构转换为Go

C structure:

typedef struct info_s {
     int len;
     uint8_t *num;
}info_t;

extern int info_collect(int unit, info_t *info,
                        data_t *data);

Go Wrapper:

type Info struct {
    Len     int
    num     []uint8
}

//Method to convert C.info_t => Info

func (inf C.info_s) Info() Info {

   var tInf Info

   tInf.Len = int(inf.len)
   for i := 0; i < int(tInf.Len); i++ {
     tInf.num[i] = uint8(inf.num[i])
   }

   return Info{
     Len: int(inf.len),
     Info: (*C.uchar)(unsafe.Pointer(&info.num[0])),
   }
}

How do I access the uint8_t *num from go wrapper? I don't think the method defined is correct. Is the struct for num - (num []uint8) the correct way to access this?

Adding a missing piece of the puzzle. There is an API in C as well that takes the C struct as an input.

Now when I call this API, I get a runtime panic:

data := []uint8{1, 2, 3}

var inf = new(C.info_t)

inf.len = 64
inf.num = data

C.info_collect(C.int(unit), (*C.info_t)(unsafe.Pointer(&info)),
                            (*C.data_t)(unsafe.Pointer(&data)))

Panic: runtime error: cgo argument has Go pointer to Go pointer
  • 写回答

1条回答 默认 最新

  • dongtan9253 2018-06-29 07:10
    关注

    In Go, num is simply a slice of type []uint8 (or its alias []byte) of length len(num). To copy data from C type info_t to Go managed memory type []uint8, write a Go num function:

    package main
    
    import (
        "fmt"
        "unsafe"
    )
    
    /*
    #include <stdint.h>
    typedef struct info_s {
        int len;
        uint8_t *num;
    } info_t;
    */
    import "C"
    
    func num(info C.info_t) []uint8 {
        n := make([]uint8, 0, info.len)
        for i := _Ctype_int(0); i < info.len; i++ {
            u8 := *(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(info.num)) + uintptr(i)))
            n = append(n, u8)
        }
        return n
    }
    
    func main() {
        test := []uint8{1, 2, 3}
        fmt.Println(len(test), test)
    
        info := C.info_t{
            len: _Ctype_int(len(test)),
            num: (*_Ctype_uint8_t)(&test[0]),
        }
        infonum := (*[1 << 20]uint8)(unsafe.Pointer(info.num))[:info.len]
        fmt.Println(info.len, infonum)
    
        n := num(info)
        fmt.Println(len(n), n)
    }
    

    Output:

    3 [1 2 3]
    3 [1 2 3]
    3 [1 2 3]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器