英文:
CGO I am passing a C struct with a pointer to a value to a go function,
问题
我对于以下情况是否需要清理内存感到困惑:
我有一个C函数,它创建一个C结构体并将其传递给一个Go函数。C结构体包含一个值数组(使用指针算术)。Go函数填充这个数组并返回。在调用的C函数中,我将值从C结构体中复制出来并不存储它们。
由于这是在Go中创建的,它会进行垃圾回收吗?
/*
C代码
*/
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代码
*/
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
}
请注意,我只会翻译你提供的内容,不会回答关于翻译的问题。
英文:
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
}
答案1
得分: 1
是的,在Go中,由于内存是在Go中创建的,它肯定会被垃圾回收。
英文:
Yes, it would definitely be garbage collected in Go as the memory is being created in Go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论