简体   繁体   English

当将指针从 Struct 传递到 C 方法时,“cgo 参数具有指向 Go 指针的 Go 指针”

[英]"cgo argument has Go pointer to Go pointer" when pass pointer from Struct to C method

I am using cgo to calling C method which accept struct pointer as below:我正在使用 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

}

However, I get an error: cgo argument has Go pointer to Go pointer.但是,我得到一个错误:cgo 参数具有指向 Go 指针的 Go 指针。

I am using go version go1.16.13.我使用的是 go 版本 go1.16.13。

Is that any way to do make it work?有什么方法可以让它发挥作用吗?

Is that any way to do make it work?有什么方法可以让它发挥作用吗?

You can set the environment variable GODEBUG to cgocheck=0 .您可以将环境变量GODEBUG设置为cgocheck=0

See https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers .请参阅https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers

Keep in mind the mechanism exists to prevent C code accessing managed memory not pinned for the duration of the C function call.请记住,存在防止 C 代码访问托管 memory 在 C function 调用期间未固定的机制。 This can become relevant in the future when changes to Go's garbage collector pertaining memory relocation may be made.当 Go 的垃圾收集器发生与 memory 重定位相关的更改时,这可能会在未来变得相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM