1

I'm trying to use NumPy in a C extension for python.

I'm getting a segfault I can't explain, I've narrowed it down to this simple example.

#include "numpy/arrayobject.h"

int main()
{
    int dims[] = {1};
    double d[] = {1};

    PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, &d);
}

1 Answer 1

2

https://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html

This function must be declared so that it is visible to code outside of the routine. Besides adding the methods and constants you desire, this subroutine must also contain calls like import_array() and/or import_ufunc() depending on which C-API is needed. Forgetting to place these commands will show itself as an ugly segmentation fault (crash) as soon as any C-API subroutine is actually called.

I also had to include Py_Initialize() beforehand since this was a standalone example not being run through a Python vm as it would normally be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.