[Numpy-discussion] embedded PyArray_FromDimsAndData Segmentation Fault

Robert Kern robert.kern@gmail....
Wed May 14 18:23:30 CDT 2008


On Wed, May 14, 2008 at 5:42 PM, Thomas Hrabe <thrabe@burnham.org> wrote:
>
> Hi all,
>
> the PyArray_FromDimsAndData is still cousing me headaches.
>
> Is there anybody out there finding the error of the following code?
>
> #include "Python.h"
> #include <numpy/ndarrayobject.h>
> int main(int argc,char** argv)
> {
>
>         int dimensions = 2;
>
>         void* value = malloc(sizeof(double)*100);
>
>         int* size = (int*)malloc(sizeof(int)*2);
>
>         size[0] = 10;
>         size[1] = 10;
>
>         for(int i=0;i<100;i++)
>                 ((double*)value)[i] = 1.0;
>
>         for(int i=0;i<100;i++)
>                 printf("%e ",((double*)value)[i]);
>
>         printf("\n%d %d\n",dimensions,size[0]);
>         PyArray_FromDimsAndData(dimensions,size,NPY_DOUBLELTR,(char*)value);
> //TROUBLE HERE
>
>         return 0;
> }
>
> I allway get a segmentation fault at the PyArray_FromDimsAndData call.
> I want to create copies of c arrays, copy them into a running python
> interpreter as nd-arrays and modify them with some python functions.
>
> If I did this in a module, I would have to call the
> import_array();
> function, I know. However, this is all outside of any module and when I add
> it before PyArray_FromDimsAndData I get the following compilation error:
> src/test.cpp:24: error: return-statement with no value, in function
> returning 'int'

You can't use numpy outside of Python. Put your code into a Python
extension module. I can explain the proximate causes of the error
messages if you really want, but they aren't really relevant. The
ultimate problem is that you aren't in a Python extension module.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco


More information about the Numpy-discussion mailing list