English 中文(简体)
C CGO 从 C ** 浮上[ [ 浮出32
原标题:CGO getting a [][]float32 from C ** float
  • 时间:2012-05-23 21:33:15
  •  标签:
  • go

我试图在 C++ 库中调用 C++ 库中的函数, 并配有 C 兼容的信头, 它让我在 4x4 矩阵中通过 。

我的 Go 函数定义看起来像这个 : < stripp/ >

func GetMatrix(matrix [][]float32)

func GetMatrix(matrix []float32)

C页头是这样定义的:

void getMatrix(const float **matrix)

我尝试使用 C. GoBytes 来获得字节数组, 但从那里我有点迷路了, 因为我必须从字节数组到一组指针, 然后我又将其转换为字节数组, 并最终形成一组浮点数 。

至少我认为这是我需要做的。

我见过用C阵列数据取代Go切片的代码的例子,但我相信,在这种情况下,Go GC不会收集。 理想的矩阵将表现为正常的Go切片。

Edit: The documentation was incorrect, and the underlying C type is actually a 16 element float array.

那么问题就出现了,我能否用C.GoBytes 指针,指向一个阵列的指针,如果是这样,我如何从字节处得到一个浮流32?

问题回答

< 强度 > 编辑 此打印正确的东西

package main

/*
#include <stdio.h>
void getMatrix(const float **matrix){
    float *m = (float *)matrix;
    int i;
    for(i = 0; i<9; i++) {
        printf("%f
",m[i]);
    }
}
*/
import "C"
import "unsafe"

func main() {
    a := []float32{1,2,3,4,5,6,7,8,9}
    C.getMatrix((**C.float)(unsafe.Pointer(&a[0])))
}

在此设定一个方法, 将指针传递到 go 数组到 C 函数上, 这样 C 函数就可以将其弹出 :

package main
/*
#include <stdio.h>
void getMatrix(float *m) {
    int i;
    for(i = 0; i < 16; i++) {
        m[i] = (float)i;
    }
}
*/
import "C"
import "fmt"
func main() {
    var a [16]float32
    C.getMatrix((*C.float)(&a[0]))
    fmt.Println(a)
}

我认为你必须使用的"G"类型是

*[16]*float32

无论如何,[ 浮点32永远不符合** C的浮点。

向 4x4 阵列的指针有类型 < code_ @ [4] * [4] float32 。

在Inuart提供的答复基础上:

package main

/*
#include <stdio.h>
void getMatrix(const float **matrix){
    float *m = (float *)*matrix;
    int i;
    for(i = 0; i<16; i++) {
       printf("%f
",m[i]);
    }
}
*/
import "C"

import "unsafe"

func main() {
    // Create the contiguous 16 element array, but organise it how it is described.
    a := [4][4]float32{
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16},
    }
    m := &a // Take the pointer.
    C.getMatrix((**C.float)(unsafe.Pointer(&m))) // Take the handle and pass it.
}

这为您提供了您似乎要求的手动行为, 并有利于您在“Go”中的数据形状, 似乎是CPPI要求的, 不必仅仅因为您与 C 互连而忽略Go的使用和安全性。





相关问题
minimum work size of a goroutine [closed]

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

How do you get the terminal size in Go?

How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go

What do you use to write Go [closed]

I know its a bit too early, but I ve been trying out Go (Google s Programming Language) and its kindof annoying to write code in gedit. So, my question: What do you use to experiment with Go?

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description