[Ecls-list] C-STRINGs and lisp strings

Juan Jose Garcia Ripoll jlr at mpq.mpg.de
Tue Jun 28 09:13:23 UTC 2005


dan at sof.ch wrote:

>I don't understand why we do not simply put a NUL at
>string.fillp. When string is a simple string, the fillp will anyway
>index the char after the end of string. [...]
>
>When we just put the NUL at the end of the (visible) string, the rule
>will be very simple to understand and follows the rule of the least
>estonishement: any string you feed in the FFI is suceptible to be
>modified and this is exactly what any C programmer expects.
>  
>
Well, it depends on what you mean by the "least astonishment". If we are 
to follow UFFI (http://uffi.b9.com/manual/strings.html) you should only 
use C-STRINGs or foreign strings (i.e. arrays of characters as provided 
by the ffi)

"UFFI has functions to two types of C-compatible strings: cstring and 
foreign strings. cstrings are used only as parameters to and from 
functions. In some implementations a cstring is not a foreign type but 
rather the Lisp string itself. On other platforms a cstring is a newly 
allocated foreign vector for storing characters. The following is an 
example of using cstrings to both send and return a value."

"In contrast, foreign strings are always a foreign vector of characters 
which have memory allocated. Thus, if you need to allocate memory to 
hold the return value of a string, you must use a foreign string and not 
a cstring. The following is an example of using a foreign string for a 
return value."

So the rules are simple
    - only use c-strings and arrays of characters when calling C functions
    - convert explicitely c-strings to lisp ones and viceversa
    - never pass lisp strings directly.
    - anything else may or may not work as expected.
    - if the C function has allocated the string for you, use a (char *) 
type
    - if the C function expects a malloced string, use also (char *)

This guarantees portable code. And sorry for being so picky, but I would 
not like ECL to be incompatible with the rest of the lisp world.

Juanjo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jlr.vcf
Type: text/x-vcard
Size: 389 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/ecl-devel/attachments/20050628/0eb45cbb/attachment.vcf>


More information about the ecl-devel mailing list