golang 局部变量地址作为返回值

本文探讨了Go语言中变量地址作为返回值的特殊处理方式。通过逃逸分析,Go编译器智能判断变量是否应分配在堆或栈,确保即使局部变量作为返回值也能有效避免悬挂指针错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

golang 局部变量地址作为返回值的问题

func createNode(v int) *node{
	return &node{value:v} 
}

func main(){
	var root *node
	root=createNode(10)
	...
}

在c/c++语言中,以上的代码逻辑显然有问题,createNode函数中创建了一个局部变量(空间分配在栈上),并将其地址返回。但是当函数退出后 该局部变量被回收,因此函数返回的地址显然无效的。


但是在go语言中这么做是可以正常使用的。


原因即go语言编译器会做逃逸分析(escape analysis),自动决定把一个变量分配在栈上还是堆上,当发现变量的作用域没有跑出函数范围,就在栈上,反之则必须分配在堆上。


需要注意的是,对于动态new出来的局部变量,编译器也会根据是否有逃逸行为来决定是分配在堆还是栈,而并非直接分配在堆中。


参见:

How do I know whether a variable is allocated on the heap or the stack?

From a correctness standpoint, you don not need to know. Each variable in Go exists as long as there are references to it. The storage location chosen by the implementation is irrelevant to the semantics of the language.

The storage location does have an effect on writing efficient programs. When possible, the Go compilers will allocate variables that are local to a function in that function's stack frame. However, if the compiler cannot prove that the variable is not referenced after the function returns, then the compiler must allocate the variable on the garbage-collected heap to avoid dangling pointer errors. Also, if a local variable is very large, it might make more sense to store it on the heap rather than the stack.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值