so I am getting a segfault from ctypes on a 32 bit linux machine that I can’t reproduce on 64 bit darwin or linux.
Here is the C:
typedef struct {
void *ptr;
} doodle;
doodle C_intpointerfunction(int *a)
{
*a = 41;
doodle foo;
foo.ptr = a;
return foo;
}
which is compiled with:
gcc -c intpointerlibrary.c
gcc -shared intpointerlibrary.o -o libintpointerlib.so
and here is the Python:
import numpy as N
from ctypes import *
_libintpointer = N.ctypeslib.load_library('libintpointerlib.so','.')
_libintpointer.C_intpointerfunction.restype = c_void_p
_libintpointer.C_intpointerfunction.argtypes = [POINTER(c_int)]
def P_intpointerfunction():
lrc = c_int(0)
print "lrc before (should be 0) = "+str(lrc.value)
return_val = _libintpointer.C_intpointerfunction(byref(lrc))
print "lrc after (should be 41) = "+str(lrc.value)
return return_val
so now, when I call this function:
rc = P_intpointerfunction()
I get a segfault. I’ve tried creating a Python class to wrap the returned struct that is created on the C side with the same results. If passing back a native ctypes type (like c_int) everything works fine.. Is this truly just a problem with 32 bit Linux, or is there something I have not considered? Thanks!
Not sure why you’re setting
restype = c_void_p;works as expected for me, on Linux x86-64.