【发布时间】:2021-03-01 10:13:36
【问题描述】:
我正在尝试通过 pybind11 在 Python 中提供一些 C/C++ 代码,并且在 C/C++ 代码中,返回一个指向数组的指针,我想在 Python 中访问该数组作为列表或 NumPy 数组。
我测试了以下实现:
#include <pybind11/pybind11.h>
int* get_zeros(int n) {
int* a = new int[n];
memset(a, 0, n);
return a;
}
PYBIND11_MODULE(example, m) {
m.def("get_zeros", &get_zeros);
}
但是当我在 Python 中调用 get_zeros 时,它只返回了一个地址(如 -1375731712),那么如何在 Python 中使用 pybind11 访问该数组?
谢谢!
【问题讨论】:
标签: python c++ arrays pointers pybind11