Reallocating underlying array of slice


When appending data into slice, if the underlying array of the slice doesn’t have enough space, a new array will be allocated. Then the elements in old array will be copied into this new memory, accompanied with adding new data behind. So when using Go built-in append function, you must always keep the idea that “the array may have been changed” in mind, and be very careful about it, otherwise, it may bite you!

Let me explain it through a contrived example:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func addTail(s []int) {
  6. var ns [][]int
  7. for _, v := range []int{1, 2} {
  8. ns = append(ns, append(s, v))
  9. }
  10. fmt.Println(ns)
  11. }
  12. func main() {
  13. s1 := []int{0, 0}
  14. s2 := append(s1, 0)
  15. for _, v := range [][]int{s1, s2} {
  16. addTail(v)
  17. }
  18. }

The s1 is [0, 0], and the s2 is [0, 0, 0]; in addTail function, I want to add 1 and 2 behind the slice. So the wanted output is like this:

  1. [[0 0 1] [0 0 2]]
  2. [[0 0 0 1] [0 0 0 2]]

But the actual result is:

  1. [[0 0 1] [0 0 2]]
  2. [[0 0 0 2] [0 0 0 2]]

The operations on s1 are successful, while s2 not.

Let’s use delve to debug this issue and check the internal mechanism of slice: Add breakpoint on addTail function, and it is first hit when processing s1:

  1. (dlv) n
  2. > main.addTail() ./slice.go:8 (PC: 0x401022)
  3. 3: import (
  4. 4: "fmt"
  5. 5: )
  6. 6:
  7. 7: func addTail(s []int) {
  8. => 8: var ns [][]int
  9. 9: for _, v := range []int{1, 2} {
  10. 10: ns = append(ns, append(s, v))
  11. 11: }
  12. 12: fmt.Println(ns)
  13. 13: }
  14. (dlv) p s
  15. []int len: 2, cap: 2, [0,0]
  16. (dlv) p &s[0]
  17. (*int)(0xc82000a2a0)

The length and capacity of s1 are both 2, and the underlying array address is 0xc82000a2a0, so what happened when executing the following statement:

  1. ns = append(ns, append(s, v))

Since the length and capacity of s1 are both 2, there is no room for new buddy. To append a new value, a new array must be allocated, and it contains both [0, 0] from s1 and the new value(1 or 2). You can consider append(s, v) generated an anonymous new slice, and it is appended in ns. We can check it after running “ns = append(ns, append(s, v))“:

  1. (dlv) n
  2. > main.addTail() ./slice.go:9 (PC: 0x401217)
  3. 4: "fmt"
  4. 5: )
  5. 6:
  6. 7: func addTail(s []int) {
  7. 8: var ns [][]int
  8. => 9: for _, v := range []int{1, 2} {
  9. 10: ns = append(ns, append(s, v))
  10. 11: }
  11. 12: fmt.Println(ns)
  12. 13: }
  13. 14:
  14. (dlv) p ns
  15. [][]int len: 1, cap: 1, [
  16. [0,0,1],
  17. ]
  18. (dlv) p ns[0]
  19. []int len: 3, cap: 4, [0,0,1]
  20. (dlv) p &ns[0][0]
  21. (*int)(0xc82000e240)
  22. (dlv) p s
  23. []int len: 2, cap: 2, [0,0]
  24. (dlv) p &s[0]
  25. (*int)(0xc82000a2a0)

We can see the length of anonymous slice is 3, capacity is 4, and the underlying array address is 0xc82000e240, different from s1‘s (0xc82000a2a0). Continue executing until exit loop:

  1. (dlv) n
  2. > main.addTail() ./slice.go:12 (PC: 0x40124c)
  3. 7: func addTail(s []int) {
  4. 8: var ns [][]int
  5. 9: for _, v := range []int{1, 2} {
  6. 10: ns = append(ns, append(s, v))
  7. 11: }
  8. => 12: fmt.Println(ns)
  9. 13: }
  10. 14:
  11. 15: func main() {
  12. 16: s1 := []int{0, 0}
  13. 17: s2 := append(s1, 0)
  14. (dlv) p ns
  15. [][]int len: 2, cap: 2, [
  16. [0,0,1],
  17. [0,0,2],
  18. ]
  19. (dlv) p &ns[0][0]
  20. (*int)(0xc82000e240)
  21. (dlv) p &ns[1][0]
  22. (*int)(0xc82000e280)
  23. (dlv) p &s[0]
  24. (*int)(0xc82000a2a0)

We can see s1, ns[0] and ns[1] have 3 independent array.

Now, let’s follow the same steps to check what happened on s2:

  1. (dlv) n
  2. > main.addTail() ./slice.go:8 (PC: 0x401022)
  3. 3: import (
  4. 4: "fmt"
  5. 5: )
  6. 6:
  7. 7: func addTail(s []int) {
  8. => 8: var ns [][]int
  9. 9: for _, v := range []int{1, 2} {
  10. 10: ns = append(ns, append(s, v))
  11. 11: }
  12. 12: fmt.Println(ns)
  13. 13: }
  14. (dlv) p s
  15. []int len: 3, cap: 4, [0,0,0]
  16. (dlv) p &s[0]
  17. (*int)(0xc82000e220)

The length of s2 is 3, and capacity is 4, so there is one slot for adding new element. Check the s2 and ns‘ values after executing “ns = append(ns, append(s, v))“ the first time:

  1. (dlv)
  2. > main.addTail() ./slice.go:9 (PC: 0x401217)
  3. 4: "fmt"
  4. 5: )
  5. 6:
  6. 7: func addTail(s []int) {
  7. 8: var ns [][]int
  8. => 9: for _, v := range []int{1, 2} {
  9. 10: ns = append(ns, append(s, v))
  10. 11: }
  11. 12: fmt.Println(ns)
  12. 13: }
  13. 14:
  14. (dlv) p ns
  15. [][]int len: 1, cap: 1, [
  16. [0,0,0,1],
  17. ]
  18. (dlv) p &ns[0][0]
  19. (*int)(0xc82000e220)
  20. (dlv) p s
  21. []int len: 3, cap: 4, [0,0,0]
  22. (dlv) p &s[0]
  23. (*int)(0xc82000e220)

We can see the new anonymous slice’s array address is also 0xc82000e220, that’s because the s2 has enough space to hold new value, no new array is allocated. Check the s2 and ns again after adding 2:

  1. (dlv)
  2. > main.addTail() ./slice.go:12 (PC: 0x40124c)
  3. 7: func addTail(s []int) {
  4. 8: var ns [][]int
  5. 9: for _, v := range []int{1, 2} {
  6. 10: ns = append(ns, append(s, v))
  7. 11: }
  8. => 12: fmt.Println(ns)
  9. 13: }
  10. 14:
  11. 15: func main() {
  12. 16: s1 := []int{0, 0}
  13. 17: s2 := append(s1, 0)
  14. (dlv) p ns
  15. [][]int len: 2, cap: 2, [
  16. [0,0,0,2],
  17. [0,0,0,2],
  18. ]
  19. (dlv) p &ns[0][0]
  20. (*int)(0xc82000e220)
  21. (dlv) p &ns[1][0]
  22. (*int)(0xc82000e220)
  23. (dlv) p s
  24. []int len: 3, cap: 4, [0,0,0]
  25. (dlv) p &s[0]
  26. (*int)(0xc82000e220)

All 3 slices point to the same array, so the later value(2) will override previous item(1).

So in a conclusion, append is very tricky since it can modify the underlying array without noticing you. You must know the memory layout behind every slice clearly, else the slice can give you a big, unwanted surprise!