英文:
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辅助代码的情况下完成这个任务。
英文:
I am working with a C library that, unlike below, I do not control. I need to pass to a C function a pointer to an array that also contains pointers.
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]))
}
This code results in the error panic: runtime error: cgo argument has Go pointer to Go pointer
. I am wondering how I can rewrite the Go portion of this code so that it satisfies Cgo's pointer rules. My hope is that I can do this without having to write C helper code.
答案1
得分: 6
坏消息是,你必须用C语言来定义你的辅助函数。
好消息是,只需要两行代码。
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)
}
希望对你有所帮助!
英文:
Bad news, you have to define your helpers in C.
Good news, well it's only 2 lines.
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)
}
答案2
得分: 5
修改OneOfOne的解决方案,不使用C辅助函数,尽管在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)
}
英文:
Modifying OneOfOne's solution without the C helper functions, although it is nice to see how easy C helper functions are to write in the comments of the go file.
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论