lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Geoff Leyland wrote:
> a = darray(3, 1, 2, 3)
> 
> print(a[0], a[1], a[2], a[3])
> 
> Output with Lua is: nil	1	2	3
> and with LuaJIT: 1	2	3	4.268776586633e-319

You're accessing the FFI array out of bounds. a[3] is not defined
for a double[3] array.

Tony Finch wrote:
> Romulo <romuloab@gmail.com> wrote:
> > Couldn't you just ignore the zero'th element in the case of
> > ffi array?
> > In other words, create an array with size n+1?
>
> Or you can use the 0th element of the table.

Actually the example at http://luajit.org/ext_ffi.html#cdata
already has this suggestion. Scroll down to the text for (3).

For legacy code or code that needs to work in plain Lua, too, stay
with 1-based arrays. For new FFI code, use zero-based arrays.

Note that the array part of Lua tables is internally zero-based in
LuaJIT. I.e. there's no penalty for using t[0] (unlike plain Lua).

[No, this doesn't change the 1-based semantics of {} and #, etc.]

--Mike