Commit 93729dde authored by Stefan Behnel's avatar Stefan Behnel

make reference from memory allocation tutorial to memory views and array.array...

make reference from memory allocation tutorial to memory views and array.array support more prominent
parent 9f7c24fe
.. _array-array:
============================ ============================
Working with Python arrays Working with Python arrays
============================ ============================
......
.. highlight:: cython
.. _memory_allocation: .. _memory_allocation:
***************** *****************
Memory Allocation Memory Allocation
***************** *****************
Dynamic memory allocation is mostly a non-issue in Python. Dynamic memory allocation is mostly a non-issue in Python. Everything is an
Everything is an object, and the reference counting system and garbage collector object, and the reference counting system and garbage collector automatically
automatically return memory to the system when it is no longer being used. return memory to the system when it is no longer being used.
When it comes to more low-level data buffers, Cython has special support for
(multi-dimensional) arrays of simple types via NumPy, memory views or Python's
stdlib array type. They are full featured, garbage collected and much easier
to work with than bare pointers in C, while still retaining the speed and static
typing benefits.
See :ref:`array-array` and :ref:`memoryviews`.
In some situations, however, these objects can still incur an unacceptable
amount of overhead, which can then makes a case for doing manual memory
management in C.
Objects can be used in Cython as well, but sometimes this incurs a certain Simple C values and structs (such as a local variable ``cdef double x``) are
amount of overhead. In C, simple values and structs usually allocated on the stack and passed by value, but for larger and more
(such as a local variable ``cdef double x``) are allocated on the stack and complicated objects (e.g. a dynamically-sized list of doubles), the memory must
passed by value, but for larger more complicated objects be manually requested and released. C provides the functions :c:func`malloc`,
(e.g. a dynamically-sized list of doubles) memory must be :c:func:`realloc`, and :c:func:`free` for this purpose, which can be imported
manually requested and released. in cython from ``clibc.stdlib``. Their signatures are:
C provides the functions ``malloc``, ``realloc``, and ``free`` for this purpose,
which can be imported in cython from ``clibc.stdlib``. Their signatures are:: .. code-block:: c
void* malloc(size_t size) void* malloc(size_t size)
void* realloc(void* ptr, size_t size) void* realloc(void* ptr, size_t size)
...@@ -95,8 +104,3 @@ e.g.:: ...@@ -95,8 +104,3 @@ e.g.::
def __dealloc__(self, number): def __dealloc__(self, number):
PyMem_Free(self.data) # no-op if self.data is NULL PyMem_Free(self.data) # no-op if self.data is NULL
It should be noted that Cython has special support for (multi-dimensional)
arrays of simple types via NumPy and memory views which are more full featured
and easier to work with than pointers while still retaining the speed/static
typing benefits.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment