I'm trying to use pybind11 to bind heavy computing in C++ with python as front end (unfortunately). I need to pass string/bytes around from python to C++ and back. After reading through the documentation, I thought that "without conversion" meaning that the memory address of C++ and python should be the same when passing around, they are looking at the same data. Here is the dummy code:
In c++:
void string_fn(std::string& x) {
std::cout << "c++ value is: " << x << std::endl;
std::cout << "c++ address is: " << &x << std::endl;
}
PYBIND11_MODULE(module_name, module_handle) {
module_handle.def("string_fn", &string_fn, py::return_value_policy::reference);
}
Pass data from python:
a = "test_string" # same output with a = b"test_string"
print("python address: ", hex(id(a)))
# call c++ function
string_fn(a)
Outputs are:
python address: 0x7f268c65f240
c++ value is: test_string
c++ address is: 0x7ffff24c1830
As we can see, the value is the same but the address is different. I'm a total noob and my questions are:
- Am I printing the right thing? Or pybind11 just create a copy of python string?
- If I'm doing it wrong, how should I achieve it with zero copy?