Skip to contents

ffi_sizeof() and ffi_offsetof() give what C's sizeof and offsetof give, from zuffi's layout of the type for this platform's ABI: padding, nesting, arrays, unions, and the packed, aligned and #pragma pack attributes of a struct (design §4.2).

Usage

ffi_sizeof(lib, type)

ffi_offsetof(lib, type, member)

Arguments

lib

A handle from ffi_dlopen().

type

A C type, as text.

member

A member of a struct or union, as text; "a.b" and "a[2]" reach into nested members and arrays.

Value

A double: the size or offset in bytes.

Details

Types are C text, such as "int", "struct pt", "char *" or "double[4]", and struct, union and typedef names are those declared in the library handle. A type zuffi cannot lay out (a struct declared without its members, or with a member it cannot lay out) raises a zuffi_unsupported_error that says why.

Examples

lib <- ffi_dlopen(if (.Platform$OS.type == "windows") "ucrtbase" else "m")
ffi_sizeof(lib, "double")
#> [1] 8
ffi_sizeof(lib, "int[10]")
#> [1] 40
ffi_close(lib)