I have the following C function:
int c_sum(const int *a, int len) {
int c = 0;
for (int i=0; i<len; i++){
c += a[i];
}
return c;
}
After creating the shared lib with the following:
cc -fPIC -shared -o c_sum.so c_sum.c
I run the following Python code in Jupyter notebook
import numpy as np
import time
from ctypes import *
lib = cdll.LoadLibrary("c_sum.so")
c_sum = lib.c_sum
c_sum.restype = c_int
a = np.random.randint(1,5,6)
print(a)
c = c_sum(c_void_p(a.ctypes.data), c_int(len(a)))
print(c)
It prints the following:
[1 4 3 1 2 2]
8
What might be the problem here? It prints the sum of the first 3 numbers. I tried random arrays of different lengths, it turns out that the code always sums the first half of the numbers in the array. Thanks.