简体   繁体   中英

"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:

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.

I am using go version 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 .

See 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. This can become relevant in the future when changes to Go's garbage collector pertaining memory relocation may be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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