You initMatrix() function should start with the line, below: global calMat
These are the normal Python rules when assigning a "module-level" global... On Thu, Jul 23, 2009 at 3:47 PM, Andy Michaels<[email protected]> wrote: > Hello all, > > I have recently started using cython for a project that I am working on. > First, understand that I am relatively new to python, let alone cython which > I started to delve into today. Anyway... for this project, I need to > dynamically allocate memory for a 2D array (a la malloc(...)). > Unfortunately, I have become confused with what might be a scope issue, > however I am unsure. I will start with a quick example of my problem: > > > ********************************************** Code > ***************************************************** > # gain access to malloc, free, etc > cdef extern from "stdlib.h": > ctypedef unsigned long size_t > void free(void *ptr) > void *malloc(size_t size) > void *realloc(void *ptr, size_t size) > size_t strlen(char *s) > char *strcpy(char *dest, char *src) > > cdef struct Point: > int x > int y > > > cdef struct CalBox: > Point *upperLeft > Point *upperRight > Point *lowerRight > Point *lowerLeft > > > cdef CalBox **calMat > > def initMatrix(numRows = 6, numCols = 8): > calMat = <CalBox**>malloc(numCols*sizeof(CalBox*)) > for i in range(numCols): > calMat[i] = <CalBox*>malloc(numRows*sizeof(CalBox)) > > **************************************************************************************************************** > > As you can see, I define calMat outside of any function such that its scope > is not limited: any function in the module should be able to access its > contents. When I attempt compile the example, however, I get an error > stating: "Cannot convert 'CalBox *' to Python object" > > This, to me, would seem to imply that the function is not "seeing" calMat > and thus is trying to create a new python object. Furthermore, this only > seems to happen with pointers; defining C-style integers or python objects > in the main body of the module seem to be perfectly accessable. I have done > quite a bit of searching and tweaking, but nothing has worked. > > I expect it is a something simple that i have overlooked, however if anyone > could help, I would greatly appreciate it... In the meantime I guess Ill > have to keep the matrix at a predefined size. > > Thanks, > Andy > > _______________________________________________ > Cython-dev mailing list > [email protected] > http://codespeak.net/mailman/listinfo/cython-dev > > -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
