Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

pointer example modified and moved to the section "Working with NumPy" #103

Open
wants to merge 3 commits into from

2 participants

@macdentalr12

No description provided.

@bfroehle bfroehle commented on the diff
docs/src/tutorial/external.rst
@@ -76,4 +76,4 @@ declaration in ``math.h`` at compile time, but Cython does not parse
Just like the ``sin()`` function from the math library, it is possible
to declare and call into any C library as long as the module that
Cython generates is properly linked against the shared or static
-library.
+library.

Did you mean to remove the ending newline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 1 deletion.
  1. +1 −1  docs/src/tutorial/external.rst
  2. +38 −0 docs/src/tutorial/numpy.rst
View
2  docs/src/tutorial/external.rst
@@ -76,4 +76,4 @@ declaration in ``math.h`` at compile time, but Cython does not parse
Just like the ``sin()`` function from the math library, it is possible
to declare and call into any C library as long as the module that
Cython generates is properly linked against the shared or static
-library.
+library.
View
38 docs/src/tutorial/numpy.rst
@@ -311,3 +311,41 @@ compile-time if the type is set to :obj:`np.ndarray`, specifically it is
assumed that the data is stored in pure strided more and not in indirect
mode).
+Pass data from a C function via pointer
+==================
+
+
+Since use of pointers in C is ubiquitous, here we give a quick example of how
+to call C functions whose arguments contain pointers. Suppose you want to
+manage an array (allocate and deallocate) with NumPy, but its data are
+computed by an external C function declared in :file:`C_func_file.h`::
+ void C_func(double * CPointer, unsigned int N);
+
+where CPointer points to the array and N is its size.
+
+You can call the function in a Cython file in the following way::
+
+ cdef extern from "C_func_file.h":
+ void C_func(double *, unsigned int)
+
+ import cython
+ import numpy as np
+ cimport numpy as np
+
+ def f(arr): # 'arr' is a one-dimensional array of size N
+ # Before calling the external function, we need to check whether the
+ # memory for 'arr' is contiguous or not; if not, we store the computed
+ # data in an contiguous array and then copy the data from that array.
+ np.ndarray[np.double_t, ndim=1, mode="c"] contig_arr
+ if arr.flags.c_contiguous:
+ contig_arr = arr
+ else:
+ contig_arr = arr.copy('C')
+ C_func(<cython.double *> contig_arr.data, contig_arr.size)
+ if contig_arr is not arr:
+ arr[...] = contig_arr
+ return
+
+This way, you can have access the function more or less as a regular
+Python function while its data and associated memory gracefully managed
+by NumPy.
Something went wrong with that request. Please try again.