Looks the answer is not. But any one can confirm this?

package main

import "fmt"
import "unsafe"
import "runtime"

var ha0, ha1, ha2 uint64

func f() **int {
    for false{} // avoid f being inlined.
    
    s := make([]byte, unsafe.Sizeof((*int)(nil))) // enough memory to hold 
a pointer
    pp := (**int)(unsafe.Pointer(&s[0]))
    
    
    var ms0 runtime.MemStats
    runtime.GC()
    runtime.ReadMemStats(&ms0)
    ha0 = ms0.HeapAlloc
    
    x := 123
    *pp = &x
    
    var ms1 runtime.MemStats
    runtime.GC()
    runtime.ReadMemStats(&ms1)
    ha1 = ms1.HeapAlloc
    
    return pp
}

var a []int

func main() {
    p := f()
    
    var ms runtime.MemStats
    runtime.GC()
    runtime.ReadMemStats(&ms)
    ha2 = ms.HeapAlloc
    
    // Is the memory allocated for x in f
    // guaranteed to be reachable at this moment?
    
    fmt.Println(ha0, ha1, ha2) // 67760 67776 67760
    
    a = make([]int, 10)
    fmt.Println(**p, a)
}




On Saturday, May 26, 2018 at 10:12:05 AM UTC-4, T L wrote:
>
>
>
> package main
>
> import "fmt"
> import "unsafe"
> import "runtime"
>
> func f() **int {
>     for false{} // avoid f being inlined.
>     
>     s := make([]byte, unsafe.Sizeof((*int)(nil))) // enough memory to 
> hold a pointer
>     pp := (**int)(unsafe.Pointer(&s[0]))
>     x := 123
>     *pp = &x
>     return pp
> }
>
> var a []int
>
> func main() {
>     p := f()
>     
>     runtime.GC()
>     
>     // Is the memory allocated for x in f
>     // guaranteed to be reachable at this moment?
>     
>     a = make([]int, 10)
>     fmt.Println(**p, a)
> }
>
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to