Stack vs heap allocation of structs in Go, and how they relate to garbage collection

前端 未结 5 1067
谎友^
谎友^ 2020-11-30 16:19

I\'m new to Go and I\'m experiencing a bit of congitive dissonance between C-style stack-based programming where automatic variables live on the stack and allocated memory l

5条回答
  •  半阙折子戏
    2020-11-30 17:11

    According to Go's FAQ:

    if the compiler cannot prove that the variable is not referenced after the function returns, then the compiler must allocate the variable on the garbage-collected heap to avoid dangling pointer errors.

提交回复
热议问题