OK, maybe I spoke too soon. Here's the full situation. I'm trying to construct a Vector class. I have this function: local function makevec(ct) local tp = ffi.typeof("struct { int n; int Capacity; $ *Data; }", ct); tp = ffi.metatype(tp, kvec_mt); return tp;end some of the the metatype looks like this: local kvec_mt = { __new = function(ct, capacity) local obj = ffi.new(ct) -- I would like to allocate obj.Data here -- obj.Data = ffi.new(ffi.typeof(self.Data[0]), capacity); -- but it doesn't quite work out return obj; end,} Now, it's right here in the constructor of a new instance of an object of the type where I have the problem, or rather in the allocation of the .Data element, no matter where it occurs. I can not say: obj.Data = ffi.new(obj.Data[0], capacity) because at this moment in time, obj.Data doesn't point to anything, so when I try to dereference, I just get a crash. If I try the following: local newdata = ffi.new(ffi.typeof(self.Data[0]), nelems); I'll get: "cannot convert 'number' to 'struct 102 &'", which makes sense if it is trying to use the number (nelems) as an initializer of a single instance of the structure, rather than as a count of the number of elements to allocate. Kind of confusing as both are optional parameters. The test case looks like this: function test_pointvec() local point3 = ffi.typeof("struct {float x; float y; float z;}"); local Point3Vector = makevec(point3); local vertices = Point3Vector(); for i=1,10000 do vertices:Push(point3(i,i*2, i*3)); endend Might the answer here be to switch to a structure that looks more like this: local function makevec(ct) local tp = ffi.typeof("struct { int n; int Capacity; $ Data[?]; }", ct); tp = ffi.metatype(tp, kvec_mt); return tp;end Where I would use a VLA instead of a pointer? Or should the pointer case work as well? -- William=============================== - Shaping clay is easier than digging it out of the ground. From: william_a_adams@xxxxxxx Subject: RE: Access to parameterized type Date: Sat, 1 Sep 2012 16:19:22 -0700 To: luajit@xxxxxxxxxxxxx Perfect. Thanks. From: Mike Pall Sent: 9/1/2012 3:15 PM To: luajit@xxxxxxxxxxxxx Subject: Re: Access to parameterized type William Adams wrote: > I have need to access the type of the parameter used to construct a > parameterized structure: > local Vector = ffi.typeof("struct { int n; int Capacity; $ *Data; }", > ct); > Is there any way to get at what 'ct' was from within a metamethod? > I can find out how large it is by doing ffi.sizeof(instance.Data[0]) > but, all I get out of ffi.typeof(instance.Data) is "struct 102". > Alternative, could I use "struct 102" in any meaningful way, like > ffi.new("struct 102", 100); ffi.new() takes a cdecl (a string), a ctype object or a cdata object. So just use ffi.new(instance.Data[0], ...). --Mike