It would be easy enough to use a matrix class instead of VLAs, e.g. slap on 
 

    #include <boost/numeric/ublas/matrix.hpp>
    using namespace boost::numeric::ublas;

and replace int a[n][n] with matrix<int> a(n, n) and a[i][j] with a(i,j). 

If you just want to pass in an array into your C99 code you can just lie to 
Cython, use something like

void foo(int n, int a[n][n])  

in your C99 header but declare it as

cdef extern from ...
    void foo(int n, int *a)

in Cython. 





On Thursday, October 16, 2014 2:29:14 PM UTC+1, Dima Pasechnik wrote:
>
> On 2014-10-16, Volker Braun <[email protected] <javascript:>> wrote: 
> > C99 VLA are not dynamically allocated, they exhaust the static stack 
> rather 
> > quickly. If you are writing the C part of the code yourself it is in the 
> > long run almost certainly better to put matrices on the heap. If you 
> have a 
> > maximal size for arrays you could use that as static bound. Or replace 
> them 
> > with a C++ matrix class that does the dynamic allocation (e.g. 
> > boost::numeric::ublas::matrix) 
>
> it's something I wrote 20+ years ago, and uses a lot of a[i][j] syntax. 
> Allocation was done statically. So I changed it to VLAs, but I wouldn't 
> want to spend more time on it. 
>
> It's not 100% clear how to use heap instead. 
> Apparently I can call a function 
>
> void f(int n, int a[][n]){...} 
>
> by doing (instead of `int b[n][n]; f(n,b);`) 
>
> int *b; 
> b=(int *)malloc(n*n*sizeof(int)); 
> f(n, b); 
>
> but this gives scary warnings about types: 
>
>  warning: passing argument 2 of ‘f’ from incompatible pointer type 
> [enabled by default] 
>  note: expected ‘int (*)[(sizetype)(n)]’ but argument is of type ‘int *’ 
>
> even though the code appears to be working. 
>
>
> > 
> > 
> > 
> > 
> > On Thursday, October 16, 2014 12:28:21 PM UTC+1, Dima Pasechnik wrote: 
> >> 
> >> In C99 it's possible to do dynamic allocation of multidimensional 
> arrays: 
> >> 
> >> void blah(int n) { 
> >>      int a[n][n]; 
> >> ...} 
> >> 
> >> but this does not seem to be supported in Cython. If I try 
> >> sage -cython on 
> >> 
> >> def t(int n): 
> >>    cdef int a[n][n] 
> >>    cdef int i, j 
> >>    for i in range(n): 
> >>        for j in range(n): 
> >>            a[i][j]=i+j 
> >>    return a[0][n-1] 
> >> 
> >> I get 
> >> 
> >> Error compiling Cython file: 
> >> ------------------------------------------------------------ 
> >> ... 
> >> def t(int n): 
> >>    cdef int i, j 
> >>    cdef int a[n][n] 
> >>                  ^ 
> >> ------------------------------------------------------------ 
> >> 
> >> m.pyx:3:18: Not allowed in a constant expression 
> >> 
> >> As I have C99 code I'd like to hook up to Sage, I'd really 
> >> like to know how to get around this limitation (with as little overhead 
> >> as possible) 
> >> 
> >> Thanks, 
> >> Dima 
> >> 
> >> 
> >> 
> > 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to