sizeof
The sizeof
expression returns an Int32
with the size in bytes of a given type. For example:
sizeof(Int32) # => 4
sizeof(Int64) # => 8
For Reference types, the size is the same as the size of a pointer:
# On a 64 bits machine
sizeof(Pointer(Int32)) # => 8
sizeof(String) # => 8
This is because a Reference's memory is allocated on the heap and a pointer to it is passed around. To get the effective size of a class, use instance_sizeof.
The argument to sizeof is a type and is often combined with typeof:
a = 1
sizeof(typeof(a)) # => 4
To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/reference/syntax_and_semantics/sizeof.html