Variable size/Set: Difference between revisions

(→‎{{header|TXR}}: New section.)
Line 584:
 
=={{header|TXR}}==
 
This task has many possible interpretations in many contexts.
 
For instance, there is a buffer type. When we create a buffer, we specify its length. Optionally, we can also specify how much storage is actually allocated. This will prevent re-allocations if the length is increased within that limit.
 
Here, the buffer holds eight zero bytes, but 4096 bytes is allocated to it:
 
<lang txrlisp>(make-buf 8 0 4096)</lang>
 
Another situation, in the context of FFI, is that some structure needs to achieve some size, but we don't care about all of its members. We can add anonymous padding to ensure that it meets the minimum size. For instance, suppose we want to call <code>uname</code>, and we only care about retrieving the <code>sysname</code>:
 
<pre>1> (with-dyn-lib nil
(deffi uname "uname" int ((ptr-out (struct utsname
(sysname (zarray 65 char))
(nil (array 512 uint)))))))
** warning: (expr-1:2) defun: redefining uname, which is a built-in defun
#:lib-0172
2> (defvar u (new utsname))
u
3> (uname u)
0
4> u
#S(utsname sysname "Linux" nodename nil release nil version nil machine nil
domainname nil)</pre>
 
We have specified a FFI definition for <code>utsname</code> which lays down the <code>sysname</code> member to the correct system-specific array size, and then a generous amount of padding: 512 unsigned integers.
 
The padding prevents the <code>uname</code> function from accessing beyond the end of the memory that is passed to it.
 
We can, of course, determine the exact size of <code>struct utsname</code> we can specify the padding such that we know for certain that it meets or exceeds the requirement.
 
=={{header|Ursala}}==
543

edits