go指针与C++指针用法不同,按哪个语言的用法用指针,请问如何抉择?

taatcc · · 1288 次点击
Go 编译器支持逃逸分析,编译期发现一个局部变量的指针被外部使用了,就会在堆里而不是栈里分配内存。 不习惯就不要这么用呗,Go只是允许这么用,又没说必须这么用
#3
更多评论
在官方的[effective go](https://golang.org/doc/effective_go.html#composite_literals)中有一句话 > Note that, unlike in C, it's perfectly OK to return the address of a local variable; the storage associated with the variable survives after the function returns. 所以在 go 中,返回指向局部变量的指针是没问题的。
#1
这样的话,写go和c++,要切换思维模式,挺不习惯的
#2