[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Zero-based arrays with the FFI
- From: Geoff Leyland <geoff_leyland@...>
- Date: Sat, 19 Mar 2011 06:17:44 +1300
On 18/03/2011, at 11:23 PM, Mike Pall wrote:
> Geoff Leyland wrote:
>> a = darray(3, 1, 2, 3)
>> print(a, a, a, a)
>> 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 is not defined
> for a double array.
Yep, I figured that! It was just for illustrative purposes.
> Tony Finch wrote:
>> Romulo <email@example.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.