What happens to the underlying array in a slice, when a new array is created and the old array is not referenced anymore by the slice or any other variable?
Lets say, we create an empty slice with length=3 and cap=4 and then when we try to append an element which exceeds the capacity of the slice.
slice:=make(int,3,4) slice = append(slice, 6,7) //exceeds the cap, when appending 7 fmt.Println(slice)
In the above program, what will happen to the old array, since its not referenced anymore by the slice or any other variable, will its memory be freed or will it stay in the memory until the program finishes its execution ?
If there are no live references to anywhere in that array, it will be garbage collected. If there is at least one reference to any one of the elements of the array, the array will remain in memory until that reference is gone.