fieldoffsets
fieldoffsets(type)
The byte offset of each field of a type relative to the data start. For example, we could use it in the following manner to summarize information about a struct type:
julia> structinfo(T) = [zip(fieldoffsets(T),fieldnames(T),T.types)...];
julia> structinfo(StatStruct)
12-element Array{Tuple{Int64,Symbol,DataType},1}:
(0,:device,UInt64)
(8,:inode,UInt64)
(16,:mode,UInt64)
(24,:nlink,Int64)
(32,:uid,UInt64)
(40,:gid,UInt64)
(48,:rdev,UInt64)
(56,:size,Int64)
(64,:blksize,Int64)
(72,:blocks,Int64)
(80,:mtime,Float64)
(88,:ctime,Float64)
Examples
The fieldoffsets
function in Julia returns the byte offset of each field of a given type relative to the data start. Here are some examples of how it can be used:
-
Get field offsets of a struct type:
julia> struct StatStruct device::UInt64 inode::UInt64 mode::UInt64 nlink::Int64 uid::UInt64 gid::UInt64 rdev::UInt64 size::Int64 blksize::Int64 blocks::Int64 mtime::Float64 ctime::Float64 end julia> fieldoffsets(StatStruct) 12-element Array{Int64,1}: 0 8 16 24 32 40 48 56 64 72 80 88
This example shows the byte offset of each field in the
StatStruct
type. -
Summarize information about a struct type:
julia> structinfo(T) = [zip(fieldoffsets(T), fieldnames(T), T.types)...]; julia> structinfo(StatStruct) 12-element Array{Tuple{Int64, Symbol, DataType},1}: (0, :device, UInt64) (8, :inode, UInt64) (16, :mode, UInt64) (24, :nlink, Int64) (32, :uid, UInt64) (40, :gid, UInt64) (48, :rdev, UInt64) (56, :size, Int64) (64, :blksize, Int64) (72, :blocks, Int64) (80, :mtime, Float64) (88, :ctime, Float64)
This example defines a
structinfo
function that summarizes information about a struct type by combining field offsets, field names, and field types.
Please note that the StatStruct
type used in the example is for illustration purposes and may not reflect an actual definition.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.