offsetof

An offsetof expression returns the byte offset of an instance variable in a struct or class type. It accepts the type as first argument and the instance variable name prefixed by an @ as second argument:

  1. offsetof(Type, @ivar)

This is a low-level primitive and only useful if a C API needs to directly interface with the data layout of a Crystal type.

Example:

  1. struct Foo
  2. @x = 0_i64
  3. @y = 34_i32
  4. end
  5. offsetof(Foo, @y) # => 8