Why does x3 escape? Is it even correct considering s3 and x3 gets destroyed 
in the same moment (when function main ends)?

Why x1 and x2 do not escape?


*Code:*
package main

func main() {
    // 1 case, doesn't escape, struct field is pointer
    type S1 struct {
        val *int
    }
    var x1 int
    s1 := S1{}
    s1.val = &x1

    // 2 case, doesn't escape, taking address of struct
    type S2 struct {
        val int
    }
    var x2 int
    s2 := &S2{}
    s2.val = x2

    // 3 case, escapes, taking address of struct AND struct field is pointer
    type S3 struct {
        val *int
    }
    var x3 int
    s3 := &S3{}
    s3.val = &x3
}

*Output of go build -gcflags "-m -m":*

# main
./main.go:3:6: cannot inline main: unhandled op DCLTYPE
./main.go:24:6: x3 escapes to heap:
./main.go:24:6:   flow: {heap} = &x3:
./main.go:24:6:     from &x3 (address-of) at ./main.go:26:11
./main.go:24:6:     from s3.val = &x3 (assign) at ./main.go:26:9
./main.go:24:6: moved to heap: x3
./main.go:17:8: &S2{} does not escape
./main.go:25:8: &S3{} does not escape

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/33584eac-423a-4187-ae39-11c1ed116457n%40googlegroups.com.

Reply via email to