RT @empijei@twitter.com

#golang #pop #quiz

func main() {
var val int
println(&val)
f(10000)
println(&val)
}

func f(i int) {
if i--; i == 0 {
return
}
f(i)
}

this program prints:

[playground (spoilers) play.golang.org/p/JQSqP3yV3Et]

🐦🔗: twitter.com/empijei/status/120

Follow

@diddledan huh, well that's not what I expected. Why does the pointer change?

@diddledan Also weird, the same behavior exists if you set a value play.golang.org/p/GsoZUH3ksM-
at f(65) the value is the same for the pointer. At f(66) it changes.

Some sort of reallocation? Recursion greater than 64 frames seems to cause it to reallocate memory space.

This actually seems sane since go does not want you to do pointer arithmetic. Though you can use unsafe package.

@diddledan Someone could probably explain the internals more, but I think this is basically what's going on play.golang.org/p/ZHQ-i5tnwqr

@diddledan annnnd I missed the twitter link and basically rehashed everything everyone already said!

stack grows and moves, pointers get updated automatically.

This would be bad times if you used unsafe. Everywhere else, perfectly normal and sane.

Sign in to participate in the conversation
Cybrespace

cybrespace: the social hub of the information superhighway jack in to the mastodon fediverse today and surf the dataflow through our cybrepunk, slightly glitchy web portal support us on patreon or liberapay!