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

Incorrect pointers while iteration through list with RANGE keyword #16149

Closed
sev3ryn opened this issue Jun 22, 2016 · 4 comments
Closed

Incorrect pointers while iteration through list with RANGE keyword #16149

sev3ryn opened this issue Jun 22, 2016 · 4 comments

Comments

@sev3ryn
Copy link

sev3ryn commented Jun 22, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go1.5 windows/amd64
  2. What operating system and processor architecture are you using (go env)?
    set GOARCH=amd64
    set GOBIN=
    set GOEXE=.exe
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOOS=windows
    set GOPATH=C:\Users\slisovskyi\code\go
    set GORACE=
    set GOROOT=C:\Go
    set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
    set GO15VENDOREXPERIMENT=
    set CC=gcc
    set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
    set CXX=g++
    set CGO_ENABLED=1
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.

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

Iteration through list with RANGE and sending object pointer through channel - produces incorrect pointers.
Iteration by element index don't produce such bug.
I suggest it is garbage collector issue

  1. What did you expect to see?

------PTR {Name:A}
------PTR {Name:B}
------PTR {Name:C}
------PTR {Name:D}

  1. What did you see instead?

------PTR {Name:B}
------PTR {Name:B}
------PTR {Name:D}
------PTR {Name:D}

@ALTree
Copy link
Member

ALTree commented Jun 22, 2016

Not a bug, you're using channels and range incorrectly. See the explanation at Unexpected behavior when dealing with channels.

@martisch
Copy link
Contributor

martisch commented Jun 22, 2016

looks to me as working as intended.

for _, member := range team.memberList {

the member here is a variable that gets reused for every iteration. When sending a pointer to that variable it will print whatever is in that variable at the moment its printed not what was in the variable when the pointer was taken.

If you add a delay after sending the pointer you will see that the output becomes different.
time.Sleep(100 * time.Millisecond)

With the delay the range loop usually wont be faster than the routine that selects and prints from the channel. This is only for demonstration of the effect not to be meant as solution to a problem.

@rajender
Copy link
Contributor

This working correctly.

for _, member := range team.memberList {

will create member variable once, not for every iteration.

https://play.golang.org/p/m44DU-orkq

@sev3ryn
Copy link
Author

sev3ryn commented Jun 22, 2016

Thanks guys, lot clearer right now. Sorry for raising this issue

@sev3ryn sev3ryn closed this as completed Jun 22, 2016
@golang golang locked and limited conversation to collaborators Jun 22, 2017
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

5 participants