[SciPy-User] Share memory between python an C++

Kurt Smith kwmsmith at gmail.com
Tue Oct 30 10:56:12 EDT 2012


On Mon, Oct 29, 2012 at 3:01 PM, David Warde-Farley
<wardefar at iro.umontreal.ca> wrote:
> On Sat, Oct 27, 2012 at 4:29 AM, Gael Varoquaux
> <gael.varoquaux at normalesup.org> wrote:
>
>> This file is somewhat lacking an example of passing an array as a pointer
>> to C code. This can be done by passing the '.data' attribute of the
>> array, that is converted by Cython to a pointer. The following file has
>> examples of this:
>> https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/svm/liblinear.pyx
>
> Just to add to what Gael said, IIRC the .data attribute on ndarrays
> has a somewhat uncertain future in Cython, as memory views obviate the
> need for it (is that right?). Anyway, something to keep in mind. You
> can always use the PyArray_DATA macro, I think.

Or, you can always grab the address of the 0-th element of the array, which is
more portable and does not depend on the NumPy C-API.  So for a 2-dimensional
numpy array, you would do:

def func(np.ndarray[double, ndim=2] arr):
    other_c_func(&arr[0,0], arr.size)


>
> David
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-User mailing list