我需要将 float32(或字节)的一大块从 C 传递到 Go 库。
代码如下:
package main
import (
"C"
"fmt"
)
//export PrintInt
func PrintInt(x []float32) {
fmt.Println(x)
}
func main() {}
用 go build -buildmode=c-archive foo.go 编译后
我得到了 foo.h
,这里是它的一部分:
typedef GoInt64 GoInt;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern void PrintInt(GoSlice p0);
#ifdef __cplusplus
}
#endif
您可以看到 foo.h
中的类型 GoSlice
包含指向数据的指针 (void *data
)。那么 foo.h
中的 PrintInt
的实现是否会隐式复制该数据?
最佳答案
我试图从 go 端修改数据,它反射(reflect)在 C 中。所以它们共享相同的内存。
foo.go:
package main
import (
"C"
"fmt"
)
//export PrintInt
func PrintInt(x []float32) {
fmt.Println(x)
for i := range x {
x[i] = -float32(i)
}
}
func main() {}
foo.c:
#include <stdio.h>
#include "foo.h"
int main() {
float bar[32];
for(int i = 0; i < 32; i++) {
bar[i] = i;
}
GoSlice s;
s.data = (void*)bar;
s.len = 32;
s.cap = 32;
PrintInt(s);
for(int i = 0; i < 32; i++) {
printf("%f ", bar[i]);
}
return 0;
}
使用命令:
go build -buildmode=c-archive foo.go
cc foo.c foo.a -o 123 && ./123
我得到了输出:
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31] -0.000000 -1.000000 -2.000000 -3.000000 -4.000000 -5.000000 -6.000000 -7.000000 -8.000000 -9.000000 -10.000000 -11.000000 -12.000000 -13.000000 -14.000000 -15.000000 -16.000000 -17.000000 -18.000000 -19.000000 -20.000000 -21.000000 -22.000000 -23.000000 -24.000000 -25.000000 -26.000000 -27.000000 -28.000000 -29.000000 -30.000000 -31.000000
关于go - 将 slice 从 c 传递给 golang 是否会进行内存复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646589/