lua-users home
lua-l archive

Re: luajit: any way to make array initialization zero-based?

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


Uh. Yes :)
I should've tried this first, but thought it might not work.
Well, thanks for that one!
On 6/13/2012 8:36 PM, Daurnimator wrote:
On 14 June 2012 12:47, Dimiter 'malkia' Stanev<malkia@gmail.com> wrote:
How about this workaround (ugly, but still could work):
local ffi = require( "ffi" )
ffi.cdef[[
 typedef struct vec3 {
 float x, y, z;
 } vec3;
]]
local a = ffi.new( "vec3[?]", 1024 + 1 )
local b = ffi.cast( "vec3*", ffi.cast("size_t", a) - ffi.sizeof("vec3"))
print(a)
print(b)
for k = 1, 30 do
 b[k].x = k
end
for k = 0, 29 do
 print( a[k].x )
end
The trick here is that 'a' has to be kept, so that 'b' would work (not sure,
Mike would know better)
Why not just use pointer arithmetic?
b = a-1
Same rule for keeping a around applies.

AltStyle によって変換されたページ (->オリジナル) /