alignof¶
alignof
式は、指定された型のABIアラインメント(バイト単位)を持つ Int32
を返します。例えば
alignof(Int32) # => 4
alignof(Int64) # => 8
struct Foo
def initialize(@x : Int8, @y : Int16)
end
end
@[Extern]
@[Packed]
struct Bar
def initialize(@x : Int8, @y : Int16)
end
end
alignof(Foo) # => 2
alignof(Bar) # => 1
Reference 型の場合、アラインメントはポインタのアラインメントと同じです
# On a 64-bit machine
alignof(Pointer(Int32)) # => 8
alignof(String) # => 8
これは、Reference
のメモリがヒープに割り当てられ、そのポインタが渡されるためです。クラスの有効なアラインメントを取得するには、instance_alignofを使用してください。
alignof の引数は 型 であり、多くの場合 typeof と組み合わせて使用されます
a = 1
alignof(typeof(a)) # => 4