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
The following minimal alignment properties are guaranteed:
For a variable x of any type: unsafe.Alignof(x) is at least 1.
For a variable x of struct type: unsafe.Alignof(x) is the largest of all the values unsafe.Alignof(x.f) for each field f of x, but at least 1.
For a variable x of array type: unsafe.Alignof(x) is the same as unsafe.Alignof(x[0]), but at least 1.
While intuitive, this definition doesn't address structs with blank fields (because there's no valid x.f expression to access those fields) or zero-element arrays (because x[0] is an illegal expression, as 0 is a constant that's out of the array's bounds).
It seems like these can be addressed by changing the wording to something like:
The alignment of any type is at least 1.
The alignment of a struct type is at least as large as the alignment of each of its fields.
The alignment of an array type is the same as the alignment of its element type.
(The original definition repeats the "but at least 1" wording each time, but that seems redundant since nothing else indicates that rule 1 is mutually exclusive with rules 2 and 3.)
The text was updated successfully, but these errors were encountered:
@mdempsky Some of the wording here is historic - it's not clear that a field x.f is a "variable" hence some of the repetition. Furthermore, while I like your general rephrasing, it's less precise: unsafe.Alignof doesn't work on types, only expressions (including constants):
package main
import "unsafe"
const c = 2.718
func f() int { return 0 }
func main() {
println(unsafe.Alignof(3+4))
println(unsafe.Alignof(c))
println(unsafe.Alignof(f))
// println(unsafe.Alignof(int)) // doesn't work for types
}
@griesemer Understood that unsafe.Alignof only operates on expressions rather than types (aside: I wish it supported both), but the spec defines "alignment" to be a property of types:
Computer architectures may require memory addresses to be aligned; that is, for addresses of a variable to be a multiple of a factor, the variable's type's alignment.
Hence why it seems reasonable to me to word alignment guarantees in terms of types too.
The spec (September 24, 2015) says:
While intuitive, this definition doesn't address structs with blank fields (because there's no valid x.f expression to access those fields) or zero-element arrays (because x[0] is an illegal expression, as 0 is a constant that's out of the array's bounds).
It seems like these can be addressed by changing the wording to something like:
(The original definition repeats the "but at least 1" wording each time, but that seems redundant since nothing else indicates that rule 1 is mutually exclusive with rules 2 and 3.)
The text was updated successfully, but these errors were encountered: