Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access an Array with both C++ and Python

Tags:

c++

python

arrays

I am working on a simulation of a mini-galaxy consisting of about a 100,000 stars. The visual representation I want to do in Python, the large calculations in C++. Using ctypes, I am able to call a C++ function from Python.

What I basicly want is an array that is there in the RAM, that can be accessed both by python and C++. Then upon calling a function update() in python, C++ updates the array. It is important that C++ really only changes the values in the array. Copying it all the time would become quite time consuming.

I am quite a beginner, especially in C++, So I don't really know where to find the right information, and what keywords to use. Thoughts on how to do it are of course welcome, but some informational links would also be greatly appreciated.

Best,

like image 865
renger Avatar asked Mar 16 '26 16:03

renger


1 Answers

You can build C++ python wrapper module using the python C/C++ API:

https://docs.python.org/2/extending/extending.html

I would create a C++ module (dataUpdater) using python API offering a service, lets call it, update which should receive the Python object you want to load data into.

At your Python side I would call dataUpdater.update whenever I want to load the data from C++

EDIT:

Other option is to make your C++ module to behave like a data structure offering data access services such as:

  • getValueAt(index)
  • setValueAt(index)
  • getSize()

And using it at python side:

for i in xrange(dataUpdater.getSize()):
    val = dataUpdater.getValueAt(i)
    ...
like image 51
Pablo Francisco Pérez Hidalgo Avatar answered Mar 18 '26 07:03

Pablo Francisco Pérez Hidalgo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!