passing double ** pointer to c++ library

584 views
Skip to first unread message

Jürgen Bohnert

unread,
Sep 24, 2013, 9:50:29 AM9/24/13
to cython...@googlegroups.com
Hi everyone,

I am relatively new to cython and am wondering about cython pointers to arrays. I have a c++ library that expects double ** pointers to arrays that were allocated via the 'new' operator like so:

double ** array = new double * [n_x];
for (int i=0; i<n_x; i++ ) {
     array[i] = new double [n_y];
}

On the cython-side I am allocating memory to a 'numpy.ndarray'. I have learned how to pass them as c-pointers to the first mem.address '&array[0,0]' and I could always modify the c++ library such that it accepts double * pointers and then index it via "array [n_x*y + x]" yet it would be very interesting to know wether double ** pointers can be 'passed' at all (plus it would save a lot of time rewriting my library and hunting bugs).

Could I use memoryviews for this (And how would I get pointers from c-contiguous memoryviews in the first place? Didn't find any clear example on this.).
Thank you very much in advance.

Jürgen

Robert Bradshaw

unread,
Sep 27, 2013, 5:02:54 AM9/27/13
to cython...@googlegroups.com
Cython doesn't (yet) support the array new operator; you have to use
malloc. This should be fine as long as your library is not trying to
delete/allocate these pointers itself. In that case you could do
(untested)

cdef int n_x = ...
cdef double[:,::1] memview = ...
cdef double ** array = <double**> malloc(n_x * sizeof(double*))
for i in range(n_x):
array[i] = &memview[i, 0]


- Robert

José Manuel Rodríguez Sotelo

unread,
Dec 7, 2014, 5:39:52 AM12/7/14
to cython...@googlegroups.com
Hey,

Were you able to solve your issue?

I'm facing something quite similar.

Chris Barker

unread,
Dec 8, 2014, 5:10:40 PM12/8/14
to cython-users
On Sat, Dec 6, 2014 at 9:39 PM, José Manuel Rodríguez Sotelo <rdz.s...@gmail.com> wrote:
Were you able to solve your issue?

I think I posted this already, but:

A point to pointers (**) type arrays is a fundamentally different data structure than memory views or numpy arrays -- they require "strided" data all in one block, whereas each "row" in a ** could be in an entirely different place in memory.

contiguous numpy arrays are a subset of **, so you can do this without a copy in one direction, but not the other.

This:


is an example of doing copies in both directions -- in that case, the lib can re-allocate parts of the ** itself, so passing a view from numpy could not have been robust.

note that a ** is not the same as a * with strides -- the top level is an array of pointers to each row, so you would still need to allocate that array.

Note: it might be a generally useful thing to write a wrapper of ** arrays for Cython use -- i.e. to/from numpy arrays (or memory views) as a re-usable component to contribute to Cython...

HTH,
  -Chris


 

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov
Reply all
Reply to author
Forward
0 new messages