lua-users home
lua-l archive

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


On 18/03/2011, at 11:23 PM, Mike Pall wrote:

> 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.

Yep, I figured that!  It was just for illustrative purposes.

> 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).

I read that when I first read the FFI documentation, but I missed it this time, in any case...

> 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.

... I do want it to work with plain Lua, so I'm allocating the extra element for now.