You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
Linux amd64
What did you do?
I have struct that has int64 and byte type. When byte type variable is in the middle of other types in struct , the offset function does not show the proper offset(see link below) https://play.golang.org/p/AMhf-IQDLF4
type custom struct {
a int64
b int64
c byte
d int64
e int64
f byte
g byte
}
the offset and address appropriately is
True 0 0xc000078030
True 8 0xc000078038
True 16 0xc000078040
False 24 0xc000078048 //But it should be 17 . 16+1 (a.c is byte type) =17
True 32 0xc000078050
True 40 0xc000078058
True 41 0xc000078059
True 0 0xc000078030
True 8 0xc000078038
True 16 0xc000078040
False 24 0xc000078048 //But it should be 17 . 16+1 (a.c is byte type) =17
True 32 0xc000078050
True 40 0xc000078058
True 41 0xc000078059
The text was updated successfully, but these errors were encountered:
tebrizetayi
changed the title
unsafe.Offsetof() does not work properly in struct when struct is mixed of type
unsafe.Offsetof() does not work properly in struct when struct is mixed of types int64 and byte
Nov 26, 2020
As davecheney pointed out this is working as intended. Struct fields in Go gc implementation are not packed as close as possible but are aligned. See also https://golang.org/ref/spec#System_considerations. e.g. 64bit atomics might require the int64 to be properly aligned and some architectures might also require this as they do aligned loads.
See #36606 for a discussion of potentially allowing to specify packed struct layouts in the future.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. I tried from playground.
https://play.golang.org/p/AMhf-IQDLF4
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I have struct that has int64 and byte type. When byte type variable is in the middle of other types in struct , the offset function does not show the proper offset(see link below)
https://play.golang.org/p/AMhf-IQDLF4
type custom struct {
a int64
b int64
c byte
d int64
e int64
f byte
g byte
}
the offset and address appropriately is
True 0 0xc000078030
True 8 0xc000078038
True 16 0xc000078040
False 24 0xc000078048 //But it should be 17 . 16+1 (a.c is byte type) =17
True 32 0xc000078050
True 40 0xc000078058
True 41 0xc000078059
What did you expect to see?
True 0 0xc0000a0000
True 8 0xc0000a0008
True 16 0xc0000a0010
True 17 0xc0000a0018
True 25 0xc0000a0020
True 33 0xc0000a0028
True 34 0xc0000a0029
What did you see instead?
True 0 0xc000078030
True 8 0xc000078038
True 16 0xc000078040
False 24 0xc000078048 //But it should be 17 . 16+1 (a.c is byte type) =17
True 32 0xc000078050
True 40 0xc000078058
True 41 0xc000078059
The text was updated successfully, but these errors were encountered: