Skip to contents

Creates an FFI structure type from named field types. By default, libffi uses natural alignment (each field aligned to its size). Use the .pack parameter to specify tighter packing similar to #pragma pack(n) in C.

Usage

ffi_struct(..., .pack = NULL)

Arguments

...

Named FFIType objects representing struct fields

.pack

Integer specifying packing alignment (1, 2, 4, 8, or 16), or NULL for default/natural alignment. When .pack=1, fields are byte-aligned (no padding). The dot prefix prevents collision with C struct field names.

Value

StructType object

Packing

The .pack parameter affects ffi_offsetof(), ffi_sizeof(), ffi_get_field(), and ffi_set_field().

Packed Structs By Value

Packed structs cannot be passed by value - GCC compiles functions taking __attribute__((packed)) structs to expect arguments on the stack, but libffi passes via registers. Use pointers instead.

Examples

# Natural alignment (default)
Point <- ffi_struct(x = ffi_int(), y = ffi_int())

# Packed struct (1-byte alignment)
PackedData <- ffi_struct(
  flag = ffi_uint8(),
  value = ffi_int32(),
  .pack = 1
)

# Check sizes
ffi_sizeof(Point) # Natural size
#> [1] 8
ffi_sizeof(PackedData) # Packed size (smaller)
#> [1] 5

# Note: Packed structs cannot be passed by value to C functions.
# Use pointers instead:
# ffi_function("some_func", ffi_void(), ffi_pointer())