[C++-sig] [c++-sig] How does C++ cooperate with python array?

甜瓜 littlesweetmelon at gmail.com
Mon Jun 4 08:45:09 CEST 2007


I'm sure it is a newbie question. ^_^
In my python program, it will generate a very big array stored in
array.array. The data processing is time consuming. So I want to move
it into C++, and return the result array back to python.
Could you give me a related example by using Boost.Python library? It
may like this:
============
# python code
ar = array(...)
ret = func(ar)  # call a Boost.Python exported funtion which accepts a
array.array parameter.
...

# C++ code
array func(array& ar)  // just simple array.array, not numpy.array
{
   ...
   //return array
}

BOOST_PYTHON_MODULE() {...}

============
Another concern is about the memory management. If I create a new
array in C++ part, who will take charge of memory release? And, is
there any implicit data-copying on the way that the array is passed
between C++ and python? Due to the size of the array is very large,
any unnecessary copy will lead to poor performance. Is there anything
I should pay attention to when using Boost.Python?

Thank you.

--
ShenLei



More information about the Cplusplus-sig mailing list