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

fmt: fmt.Println change the value of the pointer #51570

Closed
YogiLiu opened this issue Mar 9, 2022 · 1 comment
Closed

fmt: fmt.Println change the value of the pointer #51570

YogiLiu opened this issue Mar 9, 2022 · 1 comment

Comments

@YogiLiu
Copy link

YogiLiu commented Mar 9, 2022

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

$ go version
go version go1.17.7 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xxx\AppData\Local\go-build
set GOENV=C:\Users\xxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\xxx\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\xxx\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\xxx\AppData\Local\Temp\go-build165214742=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import "fmt"

func main() {
	a := new([0]int)
	b := new([0]int)
	fmt.Println(a == b) // Unexpected output: true
	fmt.Println(a, b)   // Output: &[] &[]

	c := new([0]int)
	d := new([0]int)
	fmt.Println(c == d) // Output: false

	e := new([1]int)
	f := new([1]int)
	fmt.Println(e == f) // Output: false
	fmt.Println(e, f)   // Output: &[0] &[0]

	g := new([1]int)
	h := new([1]int)
	fmt.Println(g == h) // Output: false
}

What did you expect to see?

Output value of line 8: false

What did you see instead?

Output value of line 8: true

@ALTree
Copy link
Member

ALTree commented Mar 9, 2022

This is allowed by the spec:

A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero. Two distinct zero-size variables may have the same address in memory.

Stress on may. It can happen, but it's not mandatory, and using these zero-sized values in certain ways can cause it to not happen anymore (e.g. stack-allocated vs heap-allocated because they escape).

@ALTree ALTree closed this as completed Mar 9, 2022
@golang golang locked and limited conversation to collaborators Mar 9, 2023
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