I wrote a C wrapper using the Python C API on our development machine (which is 32 bit and runs 32 bit Python 2.7). I have since had to move the wrapper and C source code to another machine which is 64 bit and runs 64 bit Python 2.6. The following code in the C wrapper returns a segmentation fault on the 64 bit machine, but not on the 32 bit machine (it works perfectly there):
static PyObject *tide3_tide_hours(PyObject *self, PyObject *args) {
double initHour, z0, delt;
char f_seasonal;
int numHours;
// Parse arguments from Python
if (!PyArg_ParseTuple(args, "ddsid", &initHour, &z0,
&f_seasonal, &numHours, &delt))
return NULL;
// Allocate memory for zhr, which is the output we want
zhr = (double *) malloc ((numHours) *sizeof (double));
// Call the C source code
int value = tide_hours(&tc, initHour,
z0, f_seasonal, numHours, zhr, delt);
// Construct the Python return object by defining
// a new list and looping over zhr
int i;
PyObject *l = PyList_New(0);
if (!l) {
printf("No list \n");
return NULL;
}
for (i=0; i < numHours; i++) {
PyList_Append(l,PyFloat_FromDouble(zhr[i]));
//printf("%f \n",zhr[i]);
}
// Free memory and return the Python list containing zhr to Python
free (zhr);
return l;
}
Note that &tc is just a structure class I define in Python and initialize at the beginning of the C wrapper. I want zhr (calculated by tide_hours in the C source code) and can see correct values for it if I uncomment the printf statement in the for loop. If I insert:
printf("Size of zhr: %d\n",sizeof(zhr));
right after allocating memory for zhr, I see '4' on the 32 bit machine and '8' on the 64 bit machine. I'm sure this is the reason why the code is returning a segmentation fault, but I can't figure out how to stop that from happening.
I checked the rest of the C wrapper/Python code and narrowed the problem down to this section. The input arguments are the right value and type. As stated previously, zhr is calculated and output correctly by tide_hours (if I use printf in the for loop), but something is up with the memory allocation that has to do with this 32 to 64 bit switch. I'm unfamiliar with writing C wrappers (this is my first one), so I would like to know if there is a way to fix this or if something else I didn't even think of is causing the problem. Thanks ahead of time and let me know if you need more code.