Call a C function through the FFI interface.
Details
The method implementations accept an additional na_check argument (logical,
default TRUE). When TRUE, the function checks for NA values in arguments and
errors if found. Set to FALSE to skip NA checking for better performance
(at your own risk).
Error Handling Limitations
Important: libffi provides no error handling for the actual C function call. If the called C function crashes (segmentation fault, illegal instruction, abort, etc.), R itself will crash. This is a fundamental limitation of FFI
there is no portable way to catch such errors in C code.
Before making FFI calls, ensure:
The function pointer is valid (not NULL, points to executable code)
All pointer arguments are valid (use
ffi_is_nullto check)Array/buffer sizes are correct - buffer overruns cause undefined behavior
The CIF signature exactly matches the C function's signature
Struct layouts match between R types and C (check alignment/padding)
For debugging crashes:
Run R under a debugger:
R -d gdbEnable core dumps:
ulimit -c unlimitedUse address sanitizers when building the library being called
See also
ffi_is_null for checking pointer validity