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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -