All Manuals > Foreign Language Interface User Guide and Reference Manual > 7 Function, Macro and Variable Reference

with-coerced-pointer Macro


Executes forms with a variable bound to a dynamic-extent copy of an FLI pointer, possibly with a different type.




with-coerced-pointer (coerced-pointer &key type pointer-type) pointer &body body => last

A variable bound to a copy of pointer.
The type of the object pointed to by the temporary pointer. This keyword can be used to access the data at the pointer as a different type.
The pointer type of the temporary pointer.
A FLI pointer of which a copy is made.
A list of forms to be executed across the scope of the temporary pointer binding.
The value of the last form in body.

The macro with-coerced-pointer makes a temporary copy of a pointer, and executes a list of forms which may use the copy across the scope of the macro. Once the macro has terminated the memory allocated to the copy of the pointer is automatically freed.

The macro with-coerced-pointer evaluates body with coerced-pointer bound to a dynamic-extent copy of the FLI pointer pointer.

coerced-pointer points to the same foreign object as pointer.

If type is specified, then it must be a FLI type specifying the type that coerced-pointer points to. Alternatively, if pointer-type is specified, then it must be a FLI pointer type specifying the pointer type of coerced-pointer. If neither type nor pointer-type are specified then the type is the same as pointer.

You can use with-coerced-pointer in a similar way to casting a pointer type in C. You can also use it make a temporary FLI pointer that can be changed using incf-pointer or decf-pointer, without affecting pointer.

Note that coerced-pointer has dynamic-extent, so you should not use it after returning from body.


In the following example an array of ten integers is defined, pointed to by array-obj. The macro with-coerced-pointer is used to return the values stored in the array, without altering array-obj, or permanently tying up memory for a second pointer.

(setf array-obj
      (fli:allocate-foreign-object :type :int
                  :nelems 10
                  '(0 1 2 3 4 5 6 7 8 9)))
(fli:with-coerced-pointer (temp) array-obj
  (dotimes (x 10)
    (print (fli:dereference temp))
    (fli:incf-pointer temp)))
See also

3.4 An example of dynamic pointer allocation

Foreign Language Interface User Guide and Reference Manual - 01 Dec 2021 19:34:58