亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 C.double 數組傳遞給 Cgo 函數?

如何將 C.double 數組傳遞給 Cgo 函數?

Go
千萬里不及你 2023-05-15 10:20:46
我剛剛開始使用 CGo,我正在嘗試將數據發送到 C 庫,該庫對浮點數/雙精度數組執行統計計算。我現在想弄清楚的是如何將一組浮點數或 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 數組傳遞給代碼?
查看完整描述

2 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

將數組名傳遞給函數時,傳遞的是初始元素的位置。在被調用的函數中,這個參數是一個局部變量,所以數組名參數是一個指針,即一個包含地址的變量。

C 程序設計語言,第 2 版


切片類型

切片是底層數組的連續段的描述符,并提供對該數組中元素編號序列的訪問。

與數組一樣,切片是可索引的并且具有長度。可以通過內置函數 len 發現切片 s 的長度;與數組不同,它可能會在執行期間發生變化。這些元素可以通過整數索引 0 到 len(s)-1 尋址。給定元素的切片索引可能小于基礎數組中同一元素的索引。

切片一旦被初始化,總是與保存其元素的底層數組相關聯。

Go 編程語言規范


參考:Go 命令 cgo


對于切片a,C 函數的參數pop_mean(int numPoints, double a[])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


查看完整回答
反對 回復 2023-05-15
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

我發現您必須發送指向數組中第一個值的指針,而不是發送指向切片第一個元素或切片本身的指針。


而且我還遇到了一個問題,我創建了一個新變量,該變量被分配了切片中第一項的值,后來創建了一個指向該變量的指針(不再是原始數組的一部分),而不是創建指向數組中第一項的指針(就像我想要的那樣)。


下面是工作代碼,帶有注釋以幫助避免上面段落中的問題。


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


查看完整回答
反對 回復 2023-05-15
  • 2 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號