German Farinas
German Farinas

Reputation: 61

Segmentation fault creating PyArrayObject using PyArray_SimpleNew

I am creating a C extension for NumPy. The function should return an array, so I decided to create a PyArrayObject with dimensions 50x10 using PyArray_SimpleNew and later fill it with some values. Here is the code:

PyArrayObject *a; npy_intp dims[2];
dims[0] = 50; dims[1] = 10;
a = (PyArrayObject *) PyArray_SimpleNew(2, dims, NPY_DOUBLE); 

However, the creation of the array a in the third line produces Segmentation Fault. Any idea what could be the problem?

Upvotes: 1

Views: 840

Answers (1)

German Farinas
German Farinas

Reputation: 61

I needed to include import_array() in my init function as shown below. I don't know what import_array() does, but fix the problem.

PyMODINIT_FUNC
PyInit_multpy(void)
{
    import_array();
    return PyModule_Create(&multpymodule);
}

PS: It would be wonderful to know why import_array() has to be called in the PyMODINIT_FUNC. If somebody knows, please explain.

Upvotes: 4

Related Questions