Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] how to access raw data created in C++? #2533

Closed
IvanM76 opened this issue Sep 27, 2020 · 4 comments
Closed

[QUESTION] how to access raw data created in C++? #2533

IvanM76 opened this issue Sep 27, 2020 · 4 comments

Comments

@IvanM76
Copy link

IvanM76 commented Sep 27, 2020

How is It possibile to create raw data (Dynamic allocated Array) in C++ and passing It as a variable to interpreter in order to modify it Python side but in a way that all the modifications are reflected also in c++ side? I mean something like boost::numpy::ndarray

@YannickJadoul
Copy link
Collaborator

@IvanM76
Copy link
Author

IvanM76 commented Sep 27, 2020

thankyou Yannick for the response. My problem is that using py::array the data are copied from c++ to the py::array object.
For example in this code:

py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::object main = py::module::import("__main__");
py::object globals = main.attr("__dict__");

double* myVect = new double[100];
memset(myVect, 0, sizeof(double) * 100);
myVect[0] = 1.0;
myVect[99] = 2.0;
size_t size = 100;
py::array pyVect((size_t)100, myVect);
globals["myArray"] = pyVect;
std::cout << myVect[0] << "    ---   " << myVect[99] << std::endl;
py::exec("print(myArray)");
py::exec("myArray = myArray[::-1]");
py::exec("print(myArray)");
std::cout << myVect[0] << "    ---   " << myVect[99] << std::endl;
delete[] myVect;

the array named myArray in python is reversed, but not myVect.
Is there a way to avoid the copy of the data and instead use directly the pointer created ?
thank you

EDIT (@YannickJadoul): Surround with ```

@YannickJadoul
Copy link
Collaborator

@IvanM76, have a look at #1042, for example. You need to provide a base parameter to the py::array constructor, such that pybind11 knows that you are taking care of the memory management and that something is keeping the data alive.

In your example, where your py::array outlives the call(s) into Python, you could just pass py::none or py::capsule(nullptr). This is mostly meant for the case where you return this array to a Python caller, and you want to be sure the NumPy array's data stays valid.

@bstaletic
Copy link
Collaborator

The question seems to have been answered. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants