duandang6352 2016-11-17 01:05
浏览 87
已采纳

将[] float32转换为C * float

I want to pass the pointer to the first element of a slice []float32 to a C variable, but somehow I can't figure out how.

C Code:

typedef struct
{   const float *data_in ;
    float    *data_out ;

} SRC_DATA ;

Go:

mySlice := make([]float32, 20)
foo := C.SRC_DATA{}
foo.data_in = *C.float(&mySlice[0]) // here the program breaks

As far as I have understood, it should be possible to pass directly a pointer to the first element in the slice from Go to C, without using unsafe.Pointer().

The C function will then iterate over the slice (length known).

Any hints are appreciated!

  • 写回答

1条回答 默认 最新

  • dongpouda6700 2016-11-17 02:47
    关注
    1. You need to use a slice of C.float type instead of float32:
    package main
    /*
    #include <stdio.h>
    void test(float *in) {
        printf("C %f %f
    ", in[0], in[1]);
    }
    */
    import "C"
    
    func main() {
        in := []C.float{1.23, 4.56}
        C.test(&in[0]) // C 1.230000 4.560000
    }
    
    1. Go doesn't allow to store a Go pointer in a Go allocated structure passed to Cgo:
    package main
    /*
    #include <stdio.h>
    typedef struct {
            float *in;
            float *out;
    } SRC_DATA;
    
    void test(SRC_DATA *data) {
        printf("C %f %f
    ", data->in[0], data->in[1]);
        data->out[0] = 8.8;
        data->out[1] = 9.9;
    }
    */
    import "C"
    import "fmt"
    
    func main() {
        in := []C.float{1.23, 4.56}
        out := make([]C.float, 2)
        data := &C.SRC_DATA{in: &in[0], out: &out[0]}
        C.test(data) // panic: runtime error: cgo argument has Go pointer to Go pointer
        fmt.Println("Go", out)
    }
    

    But you can create a helper function in C:

    package main
    /*
    #include <stdio.h>
    typedef struct {
            float *in;
            float *out;
    } SRC_DATA;
    
    void test(SRC_DATA *data) {
        printf("C %f %f
    ", data->in[0], data->in[1]);
        data->out[0] = 8.8;
        data->out[1] = 9.9;
    }
    
    void test_helper(float *in, float *out) {
        SRC_DATA data;
        data.in = in;
        data.out = out;
        test(&data);
    }
    */
    import "C"
    import "fmt"
    
    func main() {
        in := []C.float{1.23, 4.56}
        out := make([]C.float, 2)
        C.test_helper(&in[0], &out[0]) // C 1.230000 4.560000
        fmt.Println("Go", out) // Go [8.8 9.9]
    }
    

    Alternatively you can allocate and free SRC_DATA structure in C:

    package main
    /*
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct {
            float *in;
            float *out;
    } SRC_DATA;
    
    void test(SRC_DATA *data) {
        printf("C %f %f
    ", data->in[0], data->in[1]);
        data->out[0] = 8.8;
        data->out[1] = 9.9;
    }
    
    SRC_DATA *alloc_src_data() {
        return (SRC_DATA*)malloc(sizeof(SRC_DATA));
    }
    
    void free_src_data(SRC_DATA *p) {
        free(p);
    }
    */
    import "C"
    import "fmt"
    
    
    func main() {
        in := []C.float{1.23, 4.56}
        out := make([]C.float, 2)
        data := C.alloc_src_data()
        defer C.free_src_data(data)
        data.in = &in[0]
        data.out = &out[0]
        C.test(data)  // C 1.230000 4.560000
        fmt.Println("Go", out)  // Go [8.8 9.9]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab