IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

size_of

def size_of[type: AnyType, target: CompilationTarget = CompilationTarget.current()]() -> Int

Returns the size of (in bytes) of the type.

The size includes any padding required by the type's alignment, so it is always a multiple of align_of[type]() and always matches the stride between adjacent elements of an array of the type.

Example:

from std.sys.info import size_of
def main() raises:
print(
size_of[UInt8]() == 1,
size_of[UInt16]() == 2,
size_of[Int32]() == 4,
size_of[Float64]() == 8,
size_of[
SIMD[.uint8, 4]
]() == 4,
)

Note: align_of is in same module.

Parameters:

Returns:

Int: The size of the type in bytes.

def size_of[dtype: DType, target: CompilationTarget = CompilationTarget.current()]() -> Int

Returns the size of (in bytes) of the dtype.

Parameters:

Returns:

Int: The size of the dtype in bytes.