How to understand cdata type in luajit?

  • From: Nan Xiao <xiaonan830818@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 8 Sep 2015 11:08:30 +0800

Hi all,

I test the following Luajit code:

local ffi = require("ffi")

ffi.cdef[[
typedef long time_t;

typedef struct timeval {
time_t tv_sec;
time_t tv_usec;
} timeval;

int gettimeofday(struct timeval *t, void *tzp);
]]

local now = ffi.new("timeval")

ffi.C.gettimeofday(now, nil)

print(tonumber(now.tv_sec))

Execute it, and it prints the time:

1441681328

After referring the Luajit API manual (http://luajit.org/ext_ffi_api.html),
I can see the
"ffi.new" returns a "cdata" type. Since "gettimeofday" function requires a
pointer type,
could I think the cdata type be a pointer type? If not, how can I
understand cdata type?

Thanks very much in advance!

Best Regards
Nan Xiao

Other related posts: