drhozgt6007 2018-11-30 00:16
浏览 140
已采纳

如何将C.double数组传递给Cgo函数?

I'm just getting started with CGo and I'm trying to send data to a C library that performs statistical computations on arrays of floats/doubles. What I'm trying to figure out right now is how to send an array of floats, or C.double's, to a CGo function that has a signature like this:

double pop_mean(int numPoints, double a[])

I've figured out how to get in the C.int in there, but I'm having trouble figuring out how to send in an array of doubles.

I haven't yet seen any blog posts or SO Questions about this exact thing, so I thought I'd ask.

The following is my best effort so far.

// Get a basic function to work, while passing in an ARRAY  arr := make([]C.double, 0)
arr = append(arr, C.double(10.0))
arr = append(arr, C.double(20.0))
arr = append(arr, C.double(30.0))
var fixedArray [3]C.double = arr[:]

// ptr := C.CBytes(arr)
// defer C.free(unsafe.Pointer(ptr))

coolMean := C.pop_mean(3, &fixedArray)
fmt.Println("pop_mean (10, 20, 30): ", coolMean)

And this is the error I'm getting:

./main.go:64:6: cannot use arr[:] (type []_Ctype_double) as type [3]_Ctype_double in assignment
./main.go:69:35: cannot use &fixedArray (type *[3]_Ctype_double) as type *_Ctype_double in argument to _Cfunc_pop_mean

How should I be passing an array of C.double to the code?

  • 写回答

2条回答 默认 最新

  • duanmuyao0463 2018-11-30 02:06
    关注

    When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, this argument is a local variable, and so an array name parameter is a pointer, that is, a variable containing an address.

    C Programming Language, 2nd Edition


    Slice types

    A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array.

    Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

    A slice, once initialized, is always associated with an underlying array that holds its elements.

    The Go Programming Language Specification


    Reference: Go Command cgo


    For a slice a, the arguments to the pop_mean(int numPoints, double a[]) C function are len(a), the length of the slice underlying array, and &a[0], the address of the first element of the slice underlying array.


    In Go, we often hide details in a function. For example, a popMean function,

    package main
    
    import (
        "fmt"
    )
    
    /*
    double pop_mean(int numPoints, double a[]) {
        if (a == NULL || numPoints == 0) {
            return 0;
        }
        double mean = 0;
        for (int i = 0; i < numPoints; i++) {
            mean+=a[i];
        }
        return mean / numPoints;
    }
    */
    import "C"
    
    func popMean(a []float64) float64 {
        // This is the general case, which includes the special cases
        // of zero-value (a == nil and len(a) == 0)
        // and zero-length (len(a) == 0) slices.
        if len(a) == 0 {
            return 0
        }
        return float64(C.pop_mean(C.int(len(a)), (*C.double)(&a[0])))
    }
    
    func main() {
        a := make([]float64, 10)
        for i := range a {
            a[i] = float64(i + 1)
        }
    
        // slice
        fmt.Println(len(a), a)
        pm := popMean(a)
        fmt.Println(pm)
    
        // subslice
        b := a[1:4]
        fmt.Println(len(b), b)
        pm = popMean(b)
        fmt.Println(pm)
    
        // zero length
        c := a[:0]
        fmt.Println(len(c), c)
        pm = popMean(c)
        fmt.Println(pm)
    
        // zero value (nil)
        var z []float64
        fmt.Println(len(z), z, z == nil)
        pm = popMean(z)
        fmt.Println(pm)
    }
    

    Output:

    10 [1 2 3 4 5 6 7 8 9 10]
    5.5
    3 [2 3 4]
    3
    0 []
    0
    0 [] true
    0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?