CFFI variable array limitation

38 views
Skip to first unread message

Adam G

unread,
Nov 5, 2015, 6:34:11 PM11/5/15
to python-cffi
I encountered a CFFI limitation while trying to pass in a double (*)[3] to add_array:

void add_array(int x, int y, double (*a)[y], double (*b)[y], double (*result)[y])

To use this syntax, I need to define test.h as follows:

void add_array(int x, int y, double (*a)[NUM_COLS], double (*b)[NUM_COLS], double (*result)[NUM_COLS]);

And then parse NUM_COLS as follows:

ffi.cdef(my_header.read().replace('NUM_COLS', str(num_cols)))  # IMPORTANT! cffi limitation

Is it possible for CFFI to automatically infer the shape of the array?

Adam G

unread,
Nov 5, 2015, 8:19:03 PM11/5/15
to python-cffi
As a workaround, I ended up passing in a double * and performing the pointer arithmetic manually:

Ryan Gonzalez

unread,
Nov 5, 2015, 8:55:33 PM11/5/15
to pytho...@googlegroups.com, Adam G
I think this is a misunderstanding?

https://lkml.org/lkml/2015/9/3/428
--
Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity.

Armin Rigo

unread,
Nov 6, 2015, 4:14:05 AM11/6/15
to pytho...@googlegroups.com, Adam G
Hi Adam,

On Fri, Nov 6, 2015 at 2:55 AM, Ryan Gonzalez <rym...@gmail.com> wrote:
> I think this is a misunderstanding?

>>> To use this syntax, I need to define test.h as follows:
>>>
>>> void add_array(int x, int y, double (*a)[NUM_COLS], double
>>> (*b)[NUM_COLS], double (*result)[NUM_COLS]);

Yes, indeed. This is half-nonsensical C code. Don't do that and pass
plain "double *a" and "double *b" arguments and do the arithmetic
yourself---you need to anyway if the number of columns is not a
constant. That's how C works.


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages