Since buffer does not escape (.String() does a copy from internal []byte) I
would expect this buffer to be stack allocated. I'm a little confused as
the last to entries seem to conflict.
Note: I would expect buf.buf to be heap allocated (just not the struct it
belongs too).

On Thursday, March 27, 2014 3:04:19 PM UTC-5, Luke Scott wrote:

Given the following program:

package main

import "bytes"

func a() string {
var buf bytes.Buffer
buf.WriteString("Hello World")
return buf.String()

func main() {

And then run: go build -gcflags=-m test.go

I get the following results:

./test.go:8: inlining call to String
./test.go:6: moved to heap: buf
./test.go:7: buf escapes to heap
./test.go:8: a buf does not escape

Does this mean that buf is allocated on the heap? Or more to the point,
does this create garbage the GC has to clean up?
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 5 | next ›
Discussion Overview
groupgolang-nuts @
postedMar 27, '14 at 8:04p
activeMar 28, '14 at 4:04a



site design / logo © 2021 Grokbase