Note that there are some explanatory texts on larger screens.

plurals
  1. POSegFault when trying to write to a Numpy array created within a C Extension
    primarykey
    data
    text
    <p>I have an if clause within a for loop in which I have defined state_out beforehand with:</p> <pre><code>state_out = (PyArrayObject *) PyArray_FromDims(1,dims_new,NPY_BOOL); </code></pre> <p>And the if conditions are like this:</p> <pre><code> if (conn_ctr&lt;sum*2){ *(state_out-&gt;data + i*state_out-&gt;strides[0]) = true; } else { *(state_out-&gt;data + i*state_out-&gt;strides[0]) = false; } </code></pre> <p>When commenting these out, state_out returns as an all-False Numpy array. There is a problem with this assignment that I fail to see. As far as I know, all within the struct PyArrayObject that are called here in this code are pointers, so after the pointer arithmetic, it should be pointing to the address I intend to write. (All if conditions in the code are built by reaching values in this manner, and I know it works, since I managed to printf input arrays' values.) Then if I want to assign a bool to one of these parts in the memory, I should assign it via <code>*(pointer_intended) = true</code> What am I missing?</p> <p>EDIT: I have spotted that even if I don't reach those values even if I put some printf functions within:</p> <pre><code>if (conn_ctr&lt;sum*2){ printf("True!\n"); } else { printf("False!\n"); } </code></pre> <p>I get a SegFault again.</p> <p>Thanks a lot, an the rest of the code is here.</p> <pre><code>#include &lt;Python.h&gt; #include "numpy/arrayobject.h" #include &lt;stdio.h&gt; #include &lt;stdbool.h&gt; static PyObject* trace(PyObject *self, PyObject *args); static char doc[] = "This is the C extension for xor_masking routine. It interfaces with Python via C-Api, and calculates the" "next state with C pointer arithmetic"; static PyMethodDef TraceMethods[] = { {"trace", trace, METH_VARARGS, doc}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC inittrace(void) { (void) Py_InitModule("trace", TraceMethods); import_array(); } static PyObject* trace(PyObject *self, PyObject *args){ PyObject *adjacency ,*mask, *state; PyArrayObject *adjacency_arr, *mask_arr, *state_arr, *state_out; if (!PyArg_ParseTuple(args,"OOO:trace", &amp;adjacency, &amp;mask, &amp;state)) return NULL; adjacency_arr = (PyArrayObject *) PyArray_ContiguousFromObject(adjacency, NPY_BOOL,2,2); if (adjacency_arr == NULL) return NULL; mask_arr = (PyArrayObject *) PyArray_ContiguousFromObject(mask, NPY_BOOL,2,2); if (mask_arr == NULL) return NULL; state_arr = (PyArrayObject *) PyArray_ContiguousFromObject(state, NPY_BOOL,1,1); if (state_arr == NULL) return NULL; int dims[2], dims_new[1]; dims[0] = adjacency_arr -&gt; dimensions[0]; dims[1] = adjacency_arr -&gt; dimensions[1]; dims_new[0] = adjacency_arr -&gt; dimensions[0]; if (!(dims[0]==dims[1] &amp;&amp; mask_arr -&gt; dimensions[0] == dims[0] &amp;&amp; mask_arr -&gt; dimensions[1] == dims[0] &amp;&amp; state_arr -&gt; dimensions[0] == dims[0])) return NULL; state_out = (PyArrayObject *) PyArray_FromDims(1,dims_new,NPY_BOOL); int i,j; for(i=0;i&lt;dims[0];i++){ int sum = 0; int conn_ctr = 0; for(j=0;j&lt;dims[1];j++){ bool adj_value = (adjacency_arr-&gt;data + i*adjacency_arr-&gt;strides[0] +j*adjacency_arr-&gt;strides[1]); if (*(bool *) adj_value == true){ bool mask_value = (mask_arr-&gt;data + i*mask_arr-&gt;strides[0] +j*mask_arr-&gt;strides[1]); bool state_value = (state_arr-&gt;data + j*state_arr-&gt;strides[0]); if ( (*(bool *) mask_value ^ *(bool *)state_value) == true){ sum++; } conn_ctr++; } } if (conn_ctr&lt;sum*2){ } else { } } Py_DECREF(adjacency_arr); Py_DECREF(mask_arr); Py_DECREF(state_arr); return PyArray_Return(state_out); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload