Golang Cgo: panic: runtime error: cgo argument has Go pointer to Go pointer
Golang Cgo: panic: runtime error: cgo argument has Go pointer to Go pointer
我正在使用一个 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 帮助程序代码。
坏消息,你必须在 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)
}
在没有 C 辅助函数的情况下修改 OneOfOne 的解决方案,尽管很高兴看到在 go 文件的注释中编写 C 辅助函数是多么容易。
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)
}
我正在使用一个 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 帮助程序代码。
坏消息,你必须在 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)
}
在没有 C 辅助函数的情况下修改 OneOfOne 的解决方案,尽管很高兴看到在 go 文件的注释中编写 C 辅助函数是多么容易。
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)
}