i want to write a C-extension for python, as you can see below ,the aim of this code is to calculate the euclidean-dist from 2 lists of float.
the C code:
#include <python2.7/Python.h>
#include <math.h>
static PyObject* cutil_euclidean_dist(PyObject* self, PyObject* args) {
PyObject *seq_a, *seq_b;
int n;
float * array_a,* array_b;
PyObject *item;
printf("prepare to check input");
PyArg_ParseTuple(args,"iOO", &n , &seq_a, &seq_b);
if (!PySequence_Check(seq_a) || !PySequence_Check(seq_b)) {
PyErr_SetString(PyExc_TypeError, "expected sequence");
return NULL;
}
array_a =(float *)malloc(sizeof(float)*n);
array_b =(float *)malloc(sizeof(float)*n);
if (NULL == array_a || NULL == array_b){
PyErr_SetString(PyExc_TypeError, "malloc failed!");
return NULL;
}
printf("%d",array_a==NULL);
printf("%d",array_b==NULL);
printf("malloc yes ");
printf("n=%d",n);
int i;
for(i=0;i<n;i++){
printf("111");
item = PySequence_GetItem(seq_a,i);
printf("after get item");
if (!PyFloat_Check(item)) {
printf("in float check");
free(array_a); /* free up the memory before leaving */
free(array_b);
free(seq_a);
free(seq_b);
PyErr_SetString(PyExc_TypeError, "expected sequence of float");
return NULL;
}
array_a[i] = PyFloat_AsDouble(item);
printf("a=%f",array_a[i]);
Py_DECREF(item);
item = PySequence_GetItem(seq_b,i);
if(!PyFloat_Check(item)) {
free(array_a);
free(array_b);
free(seq_a);
free(seq_b);
PyErr_SetString(PyExc_TypeError, "expected sequence of float");
}
array_b[i] = PyFloat_AsDouble(item);
Py_DECREF(item);
}
printf("array analyze yes");
double sum = 0;
for(i=0;i<n;i++){
double delta = array_a[i] - array_b[i];
sum += delta * delta;
}
free(array_a);
free(array_b);
free(seq_a);
free(seq_b);
return Py_BuildValue("d",sqrt(sum));
}
static PyMethodDef cutil_methods[] = {
{"c_euclidean_dist", (PyCFunction)cutil_euclidean_dist,METH_VARARGS,NULL},
{NULL,NULL,0,NULL}
};
PyMODINIT_FUNC initcutil(void) {
Py_InitModule3("cutil", cutil_methods, "liurui's c extension for python");
}
the python code:
import cutil
cutil.c_euclidean_dist(2,[1.0,1.0],[2.0,2.0])
the result:
*** Error in `python': munmap_chunk(): invalid pointer: 0x00007fdfb8c28950 *** (core dumped)
So i can not succeed in calling this function.
who can help me with this problem? thanks a lot
i even do not know what's wrong with it.
By the way: when i complie the code, the gcc asked me to add -fPIC , but when i add -fPIC,the says gcc can not find Python.h eventually i changed:
#include <Python.h>
into this:
#include <python2.7/Python.h>
then it is OK
WHEN i comment the two sentences, the code runs well:
free(seq_a);
free(seq_b);
but i think without the 2 sentences , there will be a memory leak because the seq_a , seq_b will not be freed
You can only use free()
to deallocate memory which was allocated using malloc()
.
seq_a
and seq_b
which you are incorrectly trying to free are not allocated via malloc
.
Also, you should check out the PyObject
and reference counting chapter in Python docs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.