Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending a new value to a slice sometimes changes existing slice values #29115

Closed
myartsev opened this issue Dec 5, 2018 · 3 comments
Closed

Comments

@myartsev
Copy link

myartsev commented Dec 5, 2018

What version of Go are you using (go version)?

1.9.2 and 1.11.1
(results are different between versions!)

Does this issue reproduce with the latest release?

Yes

What did you do?

https://play.golang.org/p/EZ5u42kxiMy

What did you expect to see?

Appending values to a slice should work as expected.

What did you see instead?

Some of the existing values in the slice change after appending a new value.

Example from the play.golang code:
A slice in this state: [[] [1] [2] [3] [4] [5] [1 2]]
Gets a call to append a []int{1,3}
The resulting slice is: [[] [1] [2] [3] [4] [5] [1 3] [1 3]]
Expected: [[] [1] [2] [3] [4] [5] [1 2] [1 3]]

The perplexing part is this is only happening for some values and running the same code in different versions of go produces different outputs:

1.9.2
[[] [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 4] [2 3 4 5] [1 2 3 4 5]]

1.11.1
[[] [1] [2] [3] [4] [5] [1 2] [1 2] [1 2] [1 2] [2 3] [2 4] [2 5] [3 4] [3 4] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 4] [2 3 4 5] [1 2 3 4 5]]

Expected
[[] [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 5] [2 3 4 5] [1 2 3 4 5]]

Apologies for the somewhat convoluted example but I'm not sure how to reduce it any further.

@myartsev myartsev changed the title Appending a new values to a slice sometimes(!) changes existing slice values Appending a new value to a slice sometimes changes existing slice values Dec 5, 2018
@randall77
Copy link
Contributor

This is working as intended. Doing ret = append(ret, set) does not copy the backing store for set. You modify that backing store on the next iteration. You may want to read this if you haven't already: https://blog.golang.org/go-slices-usage-and-internals

Unlike many projects on GitHub, the Go project does not use its bug tracker for general discussion or asking questions. We only use our bug tracker for tracking bugs and tracking proposals going through the Proposal Process.

Please see https://golang.org/wiki/Questions for good places to ask questions.

@myartsev
Copy link
Author

myartsev commented Dec 5, 2018

set is a new variable on each iteration, and the same result happens with ret = append(ret, append(newBase, nums[j]))

https://play.golang.org/p/4p8c_hwCvoO

@randall77
Copy link
Contributor

set is a new variable, but its backing store is not.
It might share its backing store with newBase's backing store.

@golang golang locked and limited conversation to collaborators Dec 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants