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

How do I pass an array of C.double's to a Cgo function?

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

double pop_mean(int numPoints, double a[])

我已经弄清楚如何进入 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)

这是我遇到的错误:

./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

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

我发现你必须发送指向数组中第一个值的指针,而不是发送指向切片第一个元素或切片的指针本身。

我也 运行 遇到了我创建一个新变量的问题,该变量被分配了切片中第一项的值,后来创建了一个指向该变量的指针(不再是一部分原始数组的),而不是创建指向数组中第一项的指针(就像我想要的那样)。

下面是工作代码,带有注释以帮助避免上面段落中的问题。

// Get a basic function to work, while passing in an ARRAY

// Create a dummy array of (10,20,30), the mean of which is 20.
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))
firstValue := &(arr[0]) // this notation seems to be pretty important... Re-use this!
// if you don't make it a pointer right away, then you make a whole new object in a different location, so the contiguous-ness of the array is jeopardized.
// Because we have IMMEDIATELY made a pointer to the original value,the first value in the array, we have preserved the contiguous-ness of the array.
fmt.Println("array length: ", len(arr))

var arrayLength C.int
arrayLength = C.int(len(arr))
// arrayLength = C.int(2)

fmt.Println("array length we are using: ", arrayLength)

arrayMean := C.pop_mean(arrayLength, firstValue)
fmt.Println("pop_mean (10, 20, 30): ", arrayMean)

这会产生以下结果:

array length:  3
array length we are using:  3
pop_mean (10, 20, 30):  20

或者如果我们取消注释将 arrayLength 更改为 2 的行,我们将得到以下结果:

array length:  3
array length we are using:  2
pop_mean (10, 20, 30):  15

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


对于切片 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)
}

输出:

10 [1 2 3 4 5 6 7 8 9 10]
5.5
3 [2 3 4]
3
0 []
0
0 [] true
0