我有一些如下代码的c代码。返回值是指向动态int数组的指针。
__declspec(dllexport) int32_t* foo(const alg_real_t* x, int32_t xlen)
{
....
result = (int32_t*)malloc( n * sizeof(int32_t));
....
return result;
}
我把它编译为dll,下面是我在python中调用函数的代码:
E = cdll.LoadLibrary('mydll.dll')
E.foo.restype = POINTER(c_int)
E.foo.argtypes = [POINTER(c_int),c_int]
s = (c_int * len(mydata))(*mydata) # mydata is a python list
Result = E.foo(s,len(mydata))
当我运行代码时,我经常会收到错误信息:“WindowsError:exception:access violation reading 0xXXXXXX”。但是,有时,程序运行时没有任何错误。
任何人都可以帮助我吗?谢谢!