Open triple-Mu opened 7 months ago
According to https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#memory-view, maybe we can use memoryview to avoid copy:
py::memoryview return_from_cpp()
{
float* data = new float[5];
printf("%lld\n", data);
return py::memoryview::from_buffer(data,{1, 5},{sizeof(float) * 5, sizeof(float)});
}
from package import return_from_cpp
import numpy as np
view = return_from_cpp()
data = np.asarray(view)
print(id(data.ctypes.data))
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
2.11
Problem description
I want to return an existing data in c++ to python, so I try build a
py::array_t<float>
object by pointer. But python receive a copy array. So what should I do?Reproducible example code