python - Create numpy array from C-ptr aquired via ctypes -
i have allocated chunk of double in c library , create numpy 1d array based on data; ideally 2 versions 1 wraps c_ptr readonly - letting c layer retain ownership of data, , 1 copies data. simplified code this:
c-code
double * init_and_alloc( size_t size ) { double * ptr = malloc( size * sizeof * ptr ); // initialize ptr return ptr; }
python code
size = 1000 c_ptr = ctypes_func_ptr_init_and_alloc( size ) numpy_array = numpy.xxxx( c_ptr , size , dtype.float64) <--- ?????
so function have labelled xxxx exist?
best regards
joakim hove
yes, numpy.ctypeslib.as_array given dtype, as_array(ptr, shape).view(dtype)
.
this should work, @ least in theory (don't have time test now).
Comments
Post a Comment