Note: this is not a question. I solved it and posted it here, trying to share what I learned.
I ran into the problem of using numpy last night, and here's how I simplified it for short code. At first it looks like an error, but when I try to write this problem, I realized that it was my own error. I hope that someone else who also comes to this issue will benefit from this!
This repeats on my Win 7 x64 with the WinSDK 7.1 C compiler. Python version is 3.3.3, built with MSC v.1600. The PC version is 1.8.
0) Overview: when I pass ndarray to my dll compiled from c-code, c code sees a different array than the one I passed.
1) Write the code c:
#include <stdlib.h>
__declspec(dllexport) void copy_ndarray(double *array1, double *array2, size_t array_length);
void copy_ndarray(double *array1, double *array2, size_t array_length)
{
size_t i;
for(i=0; i<array_length; i++)
array2[i] = array1[i];
return;
}
2) Write the python code:
import numpy as np
import ctypes
lib = ctypes.cdll.LoadLibrary('./testdll.dll')
fun = lib.copy_ndarray
fun.restype = None
fun.argtypes = [np.ctypeslib.ndpointer(ctypes.c_double), np.ctypeslib.ndpointer(ctypes.c_double), ctypes.c_size_t]
array_length= 10
temp = np.c_[100.*np.ones(array_length), 200.*np.ones(array_length)]
array1 = temp[:, 1]
array2 = np.zeros(array_length)
fun(array1, array2, array_length)
3) . , array2.