Skip to content
Senior1 view

At exactly which line did slice C become detached from the underlying array, and why didn't the change `C[1] = 3000` on line 16 affect `D[0]`?

Practice

AI answer

Without a code snippet, it’s impossible to identify the exact line. A slice becomes detached from its previous underlying array at append if the addition exceeds its cap: Go allocates a new array and copies the elements into it. After that, C and D point to different arrays, so assigning C[1] = 3000 does not change D[0]. If there is sufficient capacity, append usually uses the existing array, and the slices may continue sharing data.