1 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]`?
Asked at
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.
Also asked at ЗащитаИнфоТранс, ФГУП
- Why did you decide to look for a new job?
- What amount do you base your salary expectations on?
- How does garbage collection work in Go? The three-color mark-and-sweep algorithm, STW.
- Tell about your experience, what you did, on which project you worked, and what role you played?
- What is escape analysis? In what cases does an object go into the heap?
Similar questions
- What happens if you pass a slice to a function and modify it?
- If a function accepts a slice and modifies its elements, will the original slice change? What does a slice consist of under the hood?
- How are slices passed to a function in Go?
- If you pass a slice to a function without a pointer, will it affect the original array?
- What happens if you call `append` on a slice inside a function?