英文:
Freeing C variables in Golang?
问题
我对在使用Go中的C变量时需要释放哪些变量感到困惑。
例如,如果我这样做:
s := C.CString(`something`)
那么这块内存是否一直分配到我调用C.free(unsafe.Pointer(s))
为止,还是在函数结束时由Go进行垃圾回收就可以了?
或者只有从导入的C代码创建的变量需要被释放,而从Go代码创建的这些C变量将由垃圾回收机制处理?
英文:
I'm confused about which variables need to be freed if I'm using C variables in Go.
For example, if I do this:
s := C.CString(`something`)
Is that memory now allocated until I call C.free(unsafe.Pointer(s))
, or is that OK to be garbage collected by Go when the function ends?
Or is it only variables that are created from the imported C code that need to be freed, and these C variables created from the Go code will be garbage collected?
答案1
得分: 7
// Go字符串转为C字符串
// C字符串在C堆中使用malloc分配
// 调用者有责任安排释放它,比如通过调用C.free(如果需要,请确保包含stdlib.h)。
func C.CString(string) *C.char
package cgoexample
/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%s", s);
}
*/
import "C"
import "unsafe"
func Example() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}
文章"C? Go? Cgo!"显示你不需要释放C数值类型:
func Random() int {
var r C.long = C.random()
return int(r)
}
但对于字符串,你需要释放:
import "C"
import "unsafe"
func Print(s string) {
cs := C.CString(s)
C.fputs(cs, (*C.FILE)(C.stdout))
C.free(unsafe.Pointer(cs))
}
英文:
The documentation does mention:
// Go string to C string
// The C string is allocated in the C heap using malloc.
// It is the caller's responsibility to arrange for it to be
// freed, such as by calling C.free (be sure to include stdlib.h
// if C.free is needed).
func C.CString(string) *C.char
package cgoexample
/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%s", s);
}
*/
import "C"
import "unsafe"
func Example() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}
The article "C? Go? Cgo!" shows that you don't need to free C numeric types:
func Random() int {
var r C.long = C.random()
return int(r)
}
But you would for string:
import "C"
import "unsafe"
func Print(s string) {
cs := C.CString(s)
C.fputs(cs, (*C.FILE)(C.stdout))
C.free(unsafe.Pointer(cs))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论