Skip to content

fmt.Printf("%p", &a) changes result of pointer of empty struct comparison #68701

Closed
@lwyw

Description

@lwyw

Go version

go version go1.22.5 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/weeyoung/Library/Caches/go-build'
GOENV='/Users/weeyoung/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/weeyoung/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/weeyoung/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/local/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/local/lib/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='/usr/bin/clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/weeyoung/Documents/projs/go_samples/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/gp/y_s49k7d0ks8vcy38kpltrg40000gn/T/go-build1084406235=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://go.dev/play/p/nt37zxXTC3w

What did you see happen?

Pointer comparison results are not the same after pointer print statements

	a := struct{}{}
	b := struct{}{}
	fmt.Printf("%p\n", &a)
	fmt.Printf("%p\n", &b)
	// the following produces true
	// however commenting one or all of the print statement above makes it false
	// it does not matter if the comparison is before or after the print statements of pointers
	fmt.Println(&a == &b)

What did you expect to see?

fmt.Println(&a == &b) should always produce false, the print statements should not affect its result.

Activity

changed the title [-]fmt.Printf("%p", &a) changes address of empty struct[/-] [+]fmt.Printf("%p", &a) changes result of pointer of empty struct comparison[/+] on Aug 1, 2024
lwyw

lwyw commented on Aug 1, 2024

@lwyw
Author

closing issue since it's expected

ianlancetaylor

ianlancetaylor commented on Aug 1, 2024

@ianlancetaylor
Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianlancetaylor@lwyw@gabyhelp

        Issue actions

          fmt.Printf("%p", &a) changes result of pointer of empty struct comparison · Issue #68701 · golang/go