Meaning#

LuaJIT values for pointers, structs and C numbers. ffi.string copies a C string into a Lua string; ffi.cast changes a type view without proving the address valid. Establish memory lifetime separately.

Example#

LUA
local ffi = require("ffi")
local value = ffi.new("uint64_t", 42)
local kind = type(value) -- cdata
local bytes = ffi.sizeof(value)

What happens: value is uint64_t containing 42; kind = "cdata"; bytes = 8.

This fragment does not print or write a log message. The values shown are the expected results of the example.

Methods and related calls#

Function Returns Purpose
ffi.cast cdata Creates scalar cdata by a C-type cast. Casting does not validate a memory address.
ffi.cdef none Declares C types and signatures for FFI; it does not load a library or implement a function.
ffi.copy none Copies bytes into an allocated buffer. Omitting length requires a Lua string and also copies its zero terminator.
ffi.new cdata Allocates and initializes cdata. A size argument is required for variable-length arrays or structs.
ffi.sizeof number | nil Returns the byte size of a C type or cdata; an unknown size can produce nil.
ffi.string string Copies data from a C pointer into a Lua string; without a length it reads to the zero terminator.