package main
import (
"fmt"
"os"
)
func main() {
var l = test(4)
test(5)
fmt.Fprintf(os.Stdout, "%d\n", *l)
}
func test(v int) *int {
var p = v
return &p
}
In C, the equivalent code would print 5 because the the variable p in the first stack frame would be overwritten by the same variable p in the second stack frame. I disassembled the code but am not able to make too much sense of it.
#include <stdio.h>
int* test(int v);
int main() {
int* p = test(4);
test(5);
printf("%d\n", *p);
}
int* test(int v) {
int p = v;
return &p;
}
Can somebody give me a basic synopsis of how memory management works in Go? Do function variables go on the heap?