--- title: cdata - C values and FFI summary: 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. section: reference kind: guide status: reference updated: 2026-10-26 tags: ["API","X4"] sources: ["https://wiki.egosoft.com/X4%20Foundations%20Wiki/Modding%20Support/","https://www.lua.org/manual/5.1/manual.html","https://luajit.org/ext_ffi_api.html"] --- ## 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](wiki:api-standard-ffi-cast-ae33ac09) | [`cdata`](wiki:type-cdata) | Creates scalar cdata by a C-type cast. Casting does not validate a memory address. | | [ffi.cdef](wiki:api-standard-ffi-cdef-a564999c) | [`none`](wiki:type-results) | Declares C types and signatures for FFI; it does not load a library or implement a function. | | [ffi.copy](wiki:api-standard-ffi-copy-aa0e2705) | [`none`](wiki:type-results) | Copies bytes into an allocated buffer. Omitting length requires a Lua string and also copies its zero terminator. | | [ffi.new](wiki:api-standard-ffi-new-86273c2d) | [`cdata`](wiki:type-cdata) | Allocates and initializes cdata. A size argument is required for variable-length arrays or structs. | | [ffi.sizeof](wiki:api-standard-ffi-sizeof-18d93d2f) | [`number`](wiki:type-number) \| [`nil`](wiki:type-nil) | Returns the byte size of a C type or cdata; an unknown size can produce nil. | | [ffi.string](wiki:api-standard-ffi-string-4edf893f) | [`string`](wiki:type-string) | Copies data from a C pointer into a Lua string; without a length it reads to the zero terminator. |