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

Jon*_*ele 5 go cgo

我刚刚开始使用 CGo,我正在尝试将数据发送到 C 库,该库对浮点数/双精度数数组执行统计计算。我现在想弄清楚的是如何将浮点数组或 C.double 的数组发送到具有如下签名的 CGo 函数:

double pop_mean(int numPoints, double a[])
Run Code Online (Sandbox Code Playgroud)

我已经想出了如何在那里输入 C.int,但是我在弄清楚如何发送双打数组时遇到了麻烦。

我还没有看到任何关于这件事的博客文章或 SO 问题,所以我想我会问。

以下是我迄今为止最大的努力。

// 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)
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

./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
Run Code Online (Sandbox Code Playgroud)

我应该如何将 C.double 数组传递给代码?

pet*_*rSO 6

当数组名传递给函数时,传递的是初始元素的位置。在被调用的函数中,这个参数是一个局部变量,所以数组名参数是一个指针,即一个包含地址的变量。

C 编程语言,第 2 版


切片类型

切片是底层数组的连续段的描述符,并提供对来自该数组的元素的编号序列的访问。

像数组一样,切片是可索引的并且有长度。切片 s 的长度可以通过内置函数 len 发现;与数组不同,它可能会在执行过程中发生变化。元素可以通过整数索引 0 到 len(s)-1 寻址。给定元素的切片索引可能小于底层数组中相同元素的索引。

切片一旦初始化,就始终与保存其元素的底层数组相关联。

Go 编程语言规范


参考:Go 命令 cgo


对于切片apop_mean(int numPoints, double a[])C 函数的参数是len(a),切片底层数组的长度,以及切片底层数组&a[0]的第一个元素的地址。


在 Go 中,我们经常在函数中隐藏细节。例如,一个popMean函数,

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)
}
Run Code Online (Sandbox Code Playgroud)

输出:

10 [1 2 3 4 5 6 7 8 9 10]
5.5
3 [2 3 4]
3
0 []
0
0 [] true
0
Run Code Online (Sandbox Code Playgroud)