Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

疑问-【不能将函数内的指向局部变量的指针作为返回值, 函数结束指向空间会被释放】 #7

Open
newhoo opened this issue May 27, 2022 · 0 comments

Comments

@newhoo
Copy link

newhoo commented May 27, 2022

关于这点【不能将函数内的指向局部变量的指针作为返回值, 函数结束指向空间会被释放】有疑问,示例如下:

package main

import (
	"fmt"
)

func f() *int {
	n := 100
	fmt.Println("1:", &n, n)
	return &n
}

func main() {
	n := f()
	fmt.Println("2:", n, *n)
	(*n) = 99
	fmt.Println("3:", n, *n)
}

输出:

1: 0x140000aa008 100
2: 0x140000aa008 100
3: 0x140000aa008 99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant