Как узнать, создал ли append новый базовый массив - PullRequest
0 голосов
/ 19 апреля 2019

Можно ли определить, создала ли встроенная функция append новый базовый массив?

Ответы [ 2 ]

3 голосов
/ 19 апреля 2019

Конечно, сравните емкость до и после:

before := cap(myArray)
myArray = append(myArray, newValue)
after := cap(myArray)
fmt.Printf("before: %d, after: %d", before, after)

Лучший вопрос: зачем вам это нужно?Ваш код действительно не должен заботиться о том, был ли создан новый вспомогательный массив или нет.

Демонстрация игровой площадки: https://play.golang.org/p/G_ZfrLfEpWb

0 голосов
/ 19 апреля 2019

Вы можете искать указатель памяти первого элемента.

Это изменится, если внутренняя емкость будет увеличена после степеней двух степеней.

package main

import (
    "fmt"
)

func main() {
    sl := make([]int, 0, 4)
    sl = append(sl, []int{1,2}...)  
    fmt.Printf("%#v<-%v # same pointer address to the slice length 2 with capacity 4\n", &sl[0], len(sl))
    sl = append(sl, []int{1,2}...)  
    fmt.Printf("%#v<-%v # same pointer address to the slice length 2 with capacity 4\ncapacity will change from the next append on:\n", &sl[0], len(sl))
    for i:=0; i<10; i++{
        sl = append(sl, 1)
        fmt.Printf("%#v<-len(%v) cap(%v)\n", &sl[0], len(sl), cap(sl))
    }
    for i:=0; i<10; i++{
        sl = append(sl, sl...)
        fmt.Printf("%#v<-len(%v) cap(%v)\n", &sl[0], len(sl), cap(sl))
    }
}

will print:
(*int)(0x414020)<-2 # same pointer address to the slice length 2 with capacity 4
(*int)(0x414020)<-4 # same pointer address to the slice length 2 with capacity 4
capacity will change from the next append on:
(*int)(0x450020)<-len(5) cap(8)
(*int)(0x450020)<-len(6) cap(8)
(*int)(0x450020)<-len(7) cap(8)
(*int)(0x450020)<-len(8) cap(8)
(*int)(0x432080)<-len(9) cap(16)
(*int)(0x432080)<-len(10) cap(16)
(*int)(0x432080)<-len(11) cap(16)
(*int)(0x432080)<-len(12) cap(16)
(*int)(0x432080)<-len(13) cap(16)
(*int)(0x432080)<-len(14) cap(16)
(*int)(0x456000)<-len(28) cap(32)
(*int)(0x458000)<-len(56) cap(64)
(*int)(0x45a000)<-len(112) cap(128)
(*int)(0x45c000)<-len(224) cap(256)
(*int)(0x45e000)<-len(448) cap(512)
(*int)(0x460000)<-len(896) cap(1024)
(*int)(0x462000)<-len(1792) cap(2048)
(*int)(0x464000)<-len(3584) cap(4096)
(*int)(0x468000)<-len(7168) cap(8192)
(*int)(0x470000)<-len(14336) cap(16384)

(примечание: если смотреть на небезопасный. Указатель на фрагмент не так очевиден)

...