3.1 Creating and copying pointers

Many FLI functions when evaluated return a pointer to the object created. For example, a form such as

(fli:allocate-foreign-object :type :int)

will return something similar to the following:

#<Pointer to type :INT = #x007608A0>

This is a FLI pointer object, pointing to an object at address #x007608A0 of type :int . Note that the memory address is printed in hexadecimal format, but when you use the FLI pointer functions and macros discussed in this chapter, numeric values are interpreted as base 10 unless you use Lisp reader syntax such as #x . .

To use the pointer in the future it needs to be bound to a Lisp variable. This can be done by using setq .

(setq point1 (fli:allocate-foreign-object :type :int)

However, to make a copy of a pointer it is not sufficient to do the following:

(setq point2 point1)

This simply sets point2 to contain the same pointer object as point1 . Thus if the pointer is changed using point1 , a similar change is observed when looking in point2 . To create a distinct copy of the pointer object you should use copy-pointer, which returns a new pointer object with the same address and type as the old one, as the following example shows.

(setq point3 (fli:copy-pointer point1))

A pointer can be explicitly created, rather than being returned during the allocation of memory for an FLI object, by using make-pointer. In the next example a pointer is made pointing to an :int type at the address 100 , and is bound to the Lisp variable point4 .

(setq point4 (fli:make-pointer :address 100 :type :int))

As a final note, foreign objects do take up memory. If a foreign object is no longer needed, it should be deallocated using free-foreign-object. This should be done only once for each foreign object, regardless of the number of pointer objects that contain its address. After freeing a foreign object, any pointers or copies of pointers containing its address will give unpredicable results if the memory is accessed.

LispWorks Foreign Language Interface User Guide and Reference Manual - 27 Mar 2005