Re: Accessing elements in foreign array of structs

"David McClain (as dbm at refined-audiometrics dot com)" <[email protected]> Sun, 12 Apr 2026 12:42:01 -0700
Newsgroups gmane.lisp.lispworks.general
Message-ID <[email protected]>
I do remember, myself, having wrestled with these issues back in 2008 or so. I have extensive source code showing how to construct data pointers to arbitrary types in vectors of memory - a memory mapped file access system.

It is more complicated than foreign-slot-value of a dereference. If you want the sources to study for yourself, just let me know.

- DM

> On Apr 12, 2026, at 12:24, David McClain <[email protected]> wrote:
> 
> Or perhaps you were asking about how to reference a struct slot of a struct element in an array of structs?
> 
> Doesn’t this work?
> 
> (fli:foreign-slot-value (fli:dereference array-ptr :index n) 'slot-name)
> 
> 
> 
>> On Apr 12, 2026, at 12:19, David McClain <[email protected]> wrote:
>> 
>> Er, sorry, but your question is somewhat confusing… 
>> 
>> You mentioned that your foreign function requires a points array as an input argument. Where do these points originate? 
>> 
>> From external functions? or from your running Lisp image? 
>> 
>> And if they come from Lisp, then how do you expect to provide them without first allocating and stuffing their slot values?
>> 
>> [BTW… you are aware, aren't you, that using FLI:ALLOCATE-FOREIGN-OBJECT, without an accompanying FREE, will leak memory? I always use FLI:WITH-DYNAMIC-FOREIGN-OBJECTS and then inside of it I use FLI:ALLOCATE-DYNAMIC-FOREIGN-OBJECT. This surrounds the allocations with an UNWIND-PROTECT and a forced free, of sorts.]
>> 
>>> On Apr 12, 2026, at 11:53, Erik Ronström (as erik dot ronstrom at doremir dot com) <[email protected]> wrote:
>>> 
>>> Hi,
>>> 
>>> I am calling a foreign function that takes an array of points as input.
>>> 
>>> (fli:define-c-struct (point (:foreign-name "tagPOINT"))
>>>  (x :long)
>>>  (y :long))
>>> 
>>> (let ((points (fli:allocate-foreign-object :type 'point :nelems 3)))
>>> 
>>> (the-foreign-function points))
>>> 
>>> 
>>> I can't figure out how to access the elements of the foreign array directly.
>>> 
>>> This works:
>>> 
>>> (let ((p (fli:allocate-foreign-object :type 'point)))
>>> (setf (fli:foreign-slot-value p 'x) 100
>>>       (fli:foreign-slot-value p 'y) 200
>>>       (fli:dereference points :index 0) p)
>>> 
>>> But how do I set the elements of points without using an intermediate object?
>>> 
>>> Erik
>>> 
>>> 
>>> _______________________________________________
>>> Lisp Hug - the mailing list for LispWorks users
>>> [email protected]
>>> http://www.lispworks.com/support/lisp-hug.html
>> 
>