3

I have allocated a chunk of double in a C library and I would like to create a numpy 1D array based on that data; ideally I would like two versions one which only wraps the c_ptr readonly - letting the C layer retain ownership of the data, and one which copies the data. So simplified code would be like 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 does the function I have labelled xxxx exist?

Best Regards

Joakim Hove

1 Answer 1

2

Yes, numpy.ctypeslib.as_array To get a given dtype, as_array(ptr, shape).view(dtype).

This should work, at least in theory (don't have time to test it now).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.