Heap vs Stack


Both are memory regions.

Stack

Heap

Go example:

var global *int     // "global" is the name :-)

func f() {
    var x int       // heap-allocated because escapes from f()
    x = 1
    global = &x
}

func g() {
    y := new(int)   // allocated on the stack
    *y = 1
}

More

comments powered by Disqus