[SciPy-user] Need very simple example showing how to return numpy array from C/C++ function

Nathan Bell wnbell@gmail....
Fri Dec 28 18:57:45 CST 2007

On Dec 28, 2007 6:07 PM, Jeremy Conlin <jeremit0@gmail.com> wrote:
> Now that it works, I wonder how this could be done without copying the data.
> Since a STL vector can also be accessed and treated like an array, could we
> exploit this to avoid copying?  I tried using the function
> PyArray_FromDimsAndData and passing a reference to the first element using
> the same syntax as for memcpy, but I got an error similar to the one I
> previously reported:

Ideally you could pluck out the contiguous memory chunk used by the
STL vector and hand that to numpy.  However I doubt this would work
because the vector probably needs to free its data to a different
allocator (I would assume numpy uses malloc and the STL uses some C++

> I think it would be great to have a simple example like this available to
> the community.  A page on the wiki would probably be best.  I could create
> such a page, but with my limited experience, I'm not sure I would be the
> best person.  I couldn't explain why all the elements in the *.i file are
> necessary.

Agreed.  I'd like to see if anyone else comes up with better ways of
interfacing C++ and scipy.

Matthieu, if you have time you might also include a pointer to these
wrappers for numpy's complex types:

Nathan Bell wnbell@gmail.com

More information about the SciPy-user mailing list