advanced-go-programming-book icon indicating copy to clipboard operation
advanced-go-programming-book copied to clipboard

ch2.7.2 C临时访问传入的Go内存 P111的示例没有打印输出

Open dev-go opened this issue 4 years ago • 2 comments

ch2.7.2 C临时访问传入的Go内存 P111的示例没有打印输出

Screenshot from 2020-05-15 23-01-21

dev-go avatar May 15 '20 15:05 dev-go

修改为如下代码就可以正常输出了, 难道是线程退出时没有刷新缓冲区?????? package main

/*
void printString(const char* s) {
	printf("%s", s);
        printf("\n");
}
*/
import "C"

func printString(s string) {
	cs := C.CString(s)
	defer C.free(unsafe.Pointer(cs))

	C.printString(cs)
}

func main() {
	s := "hello"
	printString(s)
}

dev-go avatar May 16 '20 11:05 dev-go

可以手工调用 fflush( stdout );试试

chai2010 avatar May 26 '20 13:05 chai2010