-
Notifications
You must be signed in to change notification settings - Fork 18k
Slice has unpredictable result #68010
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
Comments
Similar Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only. For questions please refer to https://github.com/golang/go/wiki/Questions |
This is a bug. This is not a concurrency issue. It was happened on a debug model, and I did not use any concurrency logic. |
There is a big loop (tens millions of times) out of the code, and there are tens thousands slices were created and disdroyed. Before about 3 millions of times of the loop, the slice has correct behavior. But the bug must happen before the end of the loop. |
please provide a self contained reproducer for the issue. |
In the test code, it is worse when the 'n' becomes smaller. Please see the line of
|
I'm afraid from your test program it isn't obvious that there is any bug. It would help if the program could be minimized.
That's not true. Slices can have the same pointer but different lengths, which is what appears to be happening in your error prints.
|
There is only one place of using |
The same behavior can happen using
|
For your statement, a and b are [:] model, and a is smaller. When append to b, a will not change.
This is also a different behavior with the bug code. |
I'm still not understanding what you think the bug is. |
Whatever the behavior of slice shoud be, it must be keep the same in anywhere (except for concurrency case). |
I think you are misunderstanding slices. Several slices can refer to the same underlying array. Modifications through one slice, including through the use of Think of slices as pointers.
Wrong.
You probably want to read https://go.dev/blog/slices-intro. |
I understood this sub slice case. But my case is not the sub slice case. I only need the 'c', so that I set the 'c' to everywhere. But the behavior of |
Normally the way to ensure that you never use the old slice is to always overwrite the slice you're appending to.
But in other cases it isn't.
How do you know that later on you're not appending something else to In any case, we're going to need a more demonstrable bug reproducer to reopen this issue. Just saying "it isn't doing what I expect" could be a bug in Go or a bug in your expectations. It's hard to tell because the expected behavior of the program you posted is inscrutable. |
This is second level of slice, but the bug is for first level of slice. I have no question for one level slice, and the second level of slice has no issue. The logic is complex, and I hope you can track the append code which I cannot track. Append code is black box for me...... |
Go version
go version go1.22.1 windows/amd64
Output of
go env
in your module/workspace:What did you do?
I want to use slice to store some data, which will use for some calculations.
There are two slices have the same pointer, but the data and behavior are unpredictable when I append data to them.
What did you see happen?
code:
fmt.Printf("地址(%p),值:%v \n", xz.dAA, xz.dAA)
txz := yXA[32]
fmt.Printf("地址(%p),值:%v \n", txz.dAA, txz.dAA)
output:
地址(0xc00efc9e00),值:[[[1012 36 44] [1012 50 68]] [[1013 34 71]] [[1014 32 72]] [[1015 30 73]] [[1016 22 73]] [[1017 18 74]] [[1018 14 74]] [[1019 12 74]] [[1020 34 75]] [[1021 34 75]] [[1022 32 75]]]
地址(0xc00efc9e00),值:[[[1012 36 44] [1012 50 68]] [[1013 34 71]] [[1014 32 72]] [[1015 30 73]] [[1016 22 73]] [[1017 18 74]] [[1018 14 74]] [[1019 12 74]] [[1020 34 75]] [[1021 34 75]] [[1022 32 75]] [[1023 31 47]]]
地址(0xc00efc9e00),值:[[[1012 36 44] [1012 50 68]] [[1013 34 71]] [[1014 32 72]] [[1015 30 73]] [[1016 22 73]] [[1017 18 74]] [[1018 14 74]] [[1019 12 74]] [[1020 34 75]] [[1021 34 75]] [[1022 32 75]] [[1023 68 69]]]
地址(0xc00efc9e00),值:[[[1012 36 44] [1012 50 68]] [[1013 34 71]] [[1014 32 72]] [[1015 30 73]] [[1016 22 73]] [[1017 18 74]] [[1018 14 74]] [[1019 12 74]] [[1020 34 75]] [[1021 34 75]] [[1022 32 75]] [[1023 68 69]]]
What did you expect to see?
If the slices have same pointer, then they must have same data.
The text was updated successfully, but these errors were encountered: