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

Convert slice of type A to slice of pointers to type A #22791

Closed
b1zzu opened this issue Nov 17, 2017 · 2 comments
Closed

Convert slice of type A to slice of pointers to type A #22791

b1zzu opened this issue Nov 17, 2017 · 2 comments

Comments

@b1zzu
Copy link

b1zzu commented Nov 17, 2017

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

go version go1.9 linux/amd64

Does this issue reproduce with the latest release?

Yes it can be reproduced with the last version ( 1.9 )

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/davide/.gopath"
GORACE=""
GOROOT="/usr/lib/go-1.9"
GOTOOLDIR="/usr/lib/go-1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build034477932=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

This bug can be reproduced by executing this small script

package main

import "fmt"

type A struct {
	a,b int
}

func main() {

	var a []A

	for i := 0; i < 5; i++ {
		a = append(a, A{a:i,b:i+1})
	}

	fmt.Println(a)

	var b []*A
	for _, v := range a {
		b = append(b, &v)
	}

	fmt.Println(b)

	for _, v := range b {
		fmt.Println(v)
	}
}

What did you expect to see?

I expect to have the same list of values I have i the variable a even in the variable b just that in b I will have pointers to the values.

The output that I would expect should look like this:

[{0 1} {1 2} {2 3} {3 4} {4 5}]
[0xc42001e140 0xc42001e141 0xc42001e142 0xc42001e143 0xc42001e144]
&{0 1}
&{1 2}
&{2 3}
&{3 4}
&{4 5}

What did you see instead?

This is the output i got:

[{0 1} {1 2} {2 3} {3 4} {4 5}]
[0xc42001e140 0xc42001e140 0xc42001e140 0xc42001e140 0xc42001e140]
&{4 5}
&{4 5}
&{4 5}
&{4 5}
&{4 5}

All pointers in the new slice point to the same value that is the last value of the previous slice

@b1zzu
Copy link
Author

b1zzu commented Nov 17, 2017

If a substitute the for range loop if a classic for:

var b []*A
for i := 0; i < len(a); i++ {
	b = append(b, &a[i])
}

than it work exactly as I would expect.
This is the output i get

[{0 1} {1 2} {2 3} {3 4} {4 5}]
[0xc420022100 0xc420022110 0xc420022120 0xc420022130 0xc420022140]
&{0 1}
&{1 2}
&{2 3}
&{3 4}
&{4 5}

@ALTree
Copy link
Member

ALTree commented Nov 17, 2017

This:

for _, v := range a {
	b = append(b, &v)
}

and this:

for i := 0; i < len(a); i++ {
	b = append(b, &a[i])
}

are not the same. In the latter, you are taking the address of element a[i] in the array (which is what you want). In the former, you are taking the address of the iteration variable v, which, as you discovered, does not change during the loop execution (it gets allocated once, it's in a completely new location in memory, and gets overwritten at each iteration).

Closing this, since it's not a bug.

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