[pypy-dev] Declaring a function that returns a string in CFFI
Armin Rigo
arigo at tunes.org
Wed Sep 24 19:13:00 CEST 2014
Hi,
On 23 September 2014 14:54, Eleytherios Stamatogiannakis
<estama at gmail.com> wrote:
>> p = clib.getString(...) # a "char *"
>> length = clib.strlen(p) # the standard strlen() function from C
>> b = unicode(ffi.buffer(p, length), 'utf-8')
>
> I've tried that, and the overhead of the second call is more or less equal
> to the cost of the copy when using ffi.string.
You cannot have a C function returning a 'char[]'. That's why you
need to declare it returning a 'char *', and then you don't know the
length. Sorry, it's the way C works; there is nothing I can do about
that :-)
Occasionally, we see C functions with this kind of signature:
size_t getString(xxx, char **result);
This would return the length, and use 'result' as an output parameter,
to store into '*result' a pointer to the string. If you really care
about performance, then you might want to change the C library you're
binding to in order to do that.
A bientôt,
Armin.
More information about the pypy-dev
mailing list