我正在使用一个C库,与以下不同,我无法控制。我需要向C函数传递一个指向同样包含指针的数组的指针。
package main
/*
#include <stdio.h>
typedef int* pInt;
void foo(pInt p[]) {
printf("foo()\n");
}
*/
import "C"
import "unsafe"
func main() {
var i C.int
var p1 C.pInt = (*C.int)(unsafe.Pointer(&i))
var p2 C.pInt = (*C.int)(unsafe.Pointer(&i))
var ps []C.pInt = []C.pInt{p1, p2}
C.foo(unsafe.Pointer(&ps[0]))
}
此代码会导致错误panic: runtime error: cgo argument has Go pointer to Go pointer
。我想知道如何重写此代码的Go部分,以便它满足Cgo的指针规则。我希望无需编写C帮助程序代码即可完成此操作。
答案 0 :(得分:3)
坏消息,您必须在C中定义帮助者。 好消息,它只有2行。
package main
/*
#include <stdlib.h> // for malloc/free
#include <stdio.h>
typedef int* pInt;
void foo(pInt p[]) { // you probably wanna pass a len to the function.
*p[0] = 100;
printf("foo()\n");
}
pInt * allocArray(size_t ln) { return (pInt*) malloc(ln * sizeof(pInt)); }
void freeArr(pInt * p) { free(p); }
*/
import "C"
import "unsafe"
func main() {
var (
i, sz = 0, 2
arr = C.allocArray(C.size_t(sz))
ps = (*[100000]C.pInt)(unsafe.Pointer(arr))[:sz:sz]
p1, p2 = (C.pInt)(unsafe.Pointer(&i)), (C.pInt)(unsafe.Pointer(&i))
)
ps[0], ps[1] = p1, p2
C.foo(arr)
C.freeArr(arr)
println("i", i)
}
答案 1 :(得分:3)
在没有C帮助函数的情况下修改OneOfOne的解决方案,虽然很高兴看到C帮助函数在go文件的注释中写得多么容易。
package main
/*
#include <stdio.h>
typedef int* pInt;
void foo(pInt p[]) { // you probably wanna pass a len to the function.
*p[0] = 100;
printf("foo()\n");
}
*/
import "C"
import "unsafe"
func main() {
var (
i, sz = 0, 2
arr = (*C.pInt)(C.malloc(C.size_t(sz)))
ps = (*[100000]C.pInt)(unsafe.Pointer(arr))[:sz:sz]
p1, p2 = (C.pInt)(unsafe.Pointer(&i)), (C.pInt)(unsafe.Pointer(&i))
)
ps[0], ps[1] = p1, p2
C.foo(arr)
C.free(unsafe.Pointer(arr))
println("i", i)
}