Skip to content Skip to sidebar Skip to footer

Strange Segmentation Fault In Pyarray_simplenewfromdata

My question is similar 'in spirit' to Segmentation fault in PyArray_SimpleNewFromData I have a C code that looks like this: (original code actually tests if malloc() returned NULL)

Solution 1:

I think the issue is that you're passing a Python list as the second argument to PyArray_SimpleNewFromData when it expects a pointer to an integer. I'm a little surprised this compiles.

Try:

ret = np.PyArray_SimpleNewFromData(
                     4,
                     &dim_w[0], # pointer to first element
                     np.NPY_FLOAT64,
                     dW)

Note that I've also changed the type to NPY_FLOAT64 since that should match double.

I'd also change the definition of dim_w to

np.ndarray[np.NPY_INTP, ndim=1, mode="c"] dim_w

to ensure that the type of the array matches what numpy is expecting. This may also require changing the signature of calculate_dW to double *calculate_dW(intptr_t *dim_w) to match too.


Edit: A second issue is that you need to include the line

np.import_array()

in your Cython file (just at the top level, after your imports). This does some setup stuff for numpy. In principle I think the documentation recommends you always include it when doing cimport numpy. In practice it only sometimes matter, and this is one of those times.


(Answer is now tested)

Post a Comment for "Strange Segmentation Fault In Pyarray_simplenewfromdata"