简体   繁体   中英

CGO I am passing a C struct with a pointer to a value to a go function,

I am confused as to if I need to cleanup the memory in the following scenario?

I have a C function that creates a C struct and passes it to a Go function. The C struct contains an array of values (using pointer arithmetic). The Go function populates this array and returns. In the calling C function I copy the values out of the C struct and do not store them.

As this is created in Go is this garbage collected?

/*
C code
*/
int go_func(c_struct *s);

struct c_struct{
    val *values;
    size_t *values_cnt;
};

void example_call()
{
    struct c_struct s;
    go_func(&s)
    copy_values(s)
}

/*
go code
*/

func go_func(c *C.c_struct){
    var varr *C.val

    var v C.val = createValues()
    C.set_val_in_array(varr, *v, C.size_t(0))
    c.values = varr
}

是的,因为在Go中创建内存时,肯定会在Go中收集垃圾。

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