Hi,

Ian Lance Taylor schrieb am Mittwoch, 19. Juni 2024 um 19:19:09 UTC+2:

On Wed, Jun 19, 2024 at 9:59 AM Oliver Eikemeier <eike...@fillmore-labs.com> 
wrote: 
> The specification says: “Pointers to distinct zero-size variables may or 
may not be equal.” 

The specs says, as you say above, "Pointers to distinct zero-size 
variables may or may not be equal.” That means that you can't predict 
the result of any given comparison of addresses of zero-sized 
variables.


I'm not sure I read it that way. I would interpret it as *“taking pointer 
to distinct zero-size variables may or may not result in equal pointers”*.

But not being able to have a *repeatable* result of a pointer comparison 
seems strange to me. The specification says “you'll get *some* pointers”. 
Fine.

Then I have two pointers. Where in the spec is “the result of comparison of 
pointers may change over time”?
 
For example (Go Playground <https://go.dev/play/p/acknRHBvi0P>):

func f3() {
var (
a  struct{}
b  int
aa = unsafe.Pointer(&a)
ba = unsafe.Pointer(&b)
eq = aa == ba
)

println("&a:", aa)
println("&b:", ba)
println("&a == &b:", eq)
}

gives

&a: 0xc000046738
&b: 0xc000046738
&a == &b: false 

and &b is not even a pointer to a zero-sized variable.

Could be true, could be false, could change each time you 
do the comparison. So this behavior is permitted by the spec. 


I'm not sure about the “undefined behavior *over time*”. I get that the 
compiler lays out the memory however it sees fit, but then I should have 
unchanging values in my variables.
 

> The interesting part here is that I can create two pointers (which may or 
may not be equal per specification), but depending on how I compare them I 
get different results. 

Yes, as the spec permits. 


 I assume this has little relevance in practice, but it is surprising.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/64e58302-59a6-4cbf-859e-aa6b8a2b6068n%40googlegroups.com.

Reply via email to