[英]"cgo argument has Go pointer to Go pointer" when pass pointer from Struct to C method
我正在使用 cgo 调用 C 方法,该方法接受结构指针,如下所示:
package main
/*
typedef struct Client{
struct real_client c;
} Client;
int doSomething(struct real_client *c ) {
....
}
*/
import "C"
import "fmt"
type Client struct {
client C.Client
}
func main() {
cl := Client{}
C.doSomething(&cl.client.c);
// no compile error
}
但是,我得到一个错误:cgo 参数具有指向 Go 指针的 Go 指针。
我使用的是 go 版本 go1.16.13。
有什么方法可以让它发挥作用吗?
有什么方法可以让它发挥作用吗?
您可以将环境变量GODEBUG
设置为cgocheck=0
。
请参阅https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers 。
请记住,存在防止 C 代码访问托管 memory 在 C function 调用期间未固定的机制。 当 Go 的垃圾收集器发生与 memory 重定位相关的更改时,这可能会在未来变得相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.