You can determine the size in bytes of any D type or expression,
including a struct
or
union
, by using the sizeof
operator. The sizeof
operator can be applied
either to an expression or to the name of a type surrounded by
parentheses, as illustrated in the following two examples:
sizeofexpression
sizeof (type-name
)
For example, the expression sizeof (uint64_t)
would return the value 8
, and the expression
sizeof (callinfo.ts)
would also return
8
, if inserted into the source code of the
previous example program. The formal return type of the
sizeof
operator is the type alias
size_t
, which is defined as an unsigned
integer that is the same size as a pointer in the current data
model and is used to represent byte counts. When the
sizeof
operator is applied to an expression,
the expression is validated by the D compiler, but the resulting
object size is computed at compile time and no code for the
expression is generated. You can use sizeof
anywhere an integer constant is required.
You can use the companion operator offsetof
to determine the offset in bytes of a struct or union member
from the start of the storage that is associated with any object
of the struct
or union
type. The offsetof
operator is used in an
expression of the following form:
offsetof (type-name
,member-name
)
Here, type-name
is the name of any
struct
or union
type or
type alias, and member-name
is the
identifier naming a member of that struct or union. Similar to
sizeof
, offsetof
returns a
size_t
and you can use it anywhere in a D
program that an integer constant can be used.