Call a foreign-language function: Difference between revisions
→{{header|TXR}}: Fill in.
(→{{header|TXR}}: New section.) |
(→{{header|TXR}}: Fill in.) |
||
Line 1,841:
=={{header|TXR}}==
<pre>This is the TXR Lisp interactive listener of TXR 176.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (with-dyn-lib nil
(deffi strdup "strdup" str-d (str)))
#:lib-0177
2> (strdup "hello, world!")
"hello, world!"</pre>
The requirement to free the memory is taken care of the semantics of the <code>str-d</code> ("dynamic") variant of the <code>str</code> type. The semantics denotes the passage of ownership of <code>malloc</code>-ed memory across the interface.
When the C-to-Lisp value conversion takes place on the return value, FFI releases the memory, knowing that it has received ownership of it from the function, which entails that responsibility. If the <code>str</code> type were used by mistake, a memory leak would result.
There is no way to use the <code>str</code> family of types, yet do manual memory management; FFI manages automatically. Code that wants to manually manage a foreign resource referenced by pointer should use <code>cptr</code> or <code>carray</code>, depending on required semantics.
=={{header|zkl}}==
|