在Go复制内存中执行切片分配

Purpose: I have a big buffer, and I would like to have an array/slice of pointer pointing to different loc in the buffer.

What I am doing:

datPtrs := make([][]byte, n)
for i:=0; i<n; i++{
    datPtrs[i] = bigBuf[i*m:(i+1)*m]
}

My Question:

  1. Will this copy memory? My guess is not, but I cannot find anywhere to confirm this.
  2. What is the best way/tool to find out whether there is memory copy or not?

Go slices are implemented as a struct:

src/runtime/slice.go:

type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}

You are assigning/copying the slice struct, which does not copy the underlying array, only its pointer.


A simple illustration:

package main

import (
    "fmt"
)

func main() {
    buf := make([]byte, 8)
    for i := range buf {
        buf[i] = byte(i)
    }
    sub := buf[1:3]
    fmt.Println(buf)
    fmt.Println(sub)
    for i := range sub {
        sub[i] += 43
    }
    fmt.Println(buf)
    fmt.Println(sub)
}

Playground: https://play.golang.org/p/4OzPwuNmUlY

Output:

[0 1 2 3 4 5 6 7]
[1 2]
[0 44 45 3 4 5 6 7]
[44 45]

See The Go Blog: Go Slices: usage and internals,

  1. No

Slice is just a pointer to memory + len and cap
see: Why can not I duplicate a slice with `copy()` in Golang?


  1. Like so:
package main

import (
    "fmt"
)

func main() {
    bigBuf := []byte{1, 2, 3, 4, 5}

    datPtrs := make([][]byte, 2)
    for i := 0; i < 2; i++ {
        datPtrs[i] = bigBuf[i : i+1]
    }
    fmt.Println(bigBuf) // [1 2 3 4 5]
    datPtrs[0][0] = 10
    fmt.Println(bigBuf) // [10 2 3 4 5]

    datPtrs[1][0] = 20
    fmt.Println(bigBuf) // [10 20 3 4 5]
}