C++通过PYBIND11读取Numpy阵列时的类型限制

发布于 2025-01-26 01:53:57 字数 592 浏览 3 评论 0原文

我想知道是否有人知道下面的“类型punning”是否是必要的,还是最有效的技术或方法在最后一行中?

数据是2D numpy阵列的双打阵列引用为函数参数。

侧点:如果此函数无关紧要,则最终将出现内存泄漏 /错误,因为每次在相同的相同内存地址 /位置运行函数时,都会创建PTR_SUM指针,我不会删除指针“ ptr_sum”要在指向的内存位置中删除基础数据?

double* ptr_sum =(double*)buf_summary.ptr;

void Pricing::price_all(py::array_t<double>& in_summary){

if (in_summary.ndim() != 2) {
    throw std::runtime_error("Results should be a 2-D Numpy array");
}

py::buffer_info buf_summary = in_summary.request(); // summary

// Pointer definition
double* ptr_sum = (double*)buf_summary.ptr;

I was wondering if anyone knows if the below "type punning" is necessary or if a more efficient technique or approach is best in the last line?

The data is an 2D Numpy array of doubles which is passed by reference as the function argument.

side point: If this function is called indefinetly will a memory leak/ error eventually occur as the ptr_sum pointer is being created each time the function is run on the same identical memory address / location, Im not deleting the pointer "ptr_sum" as it seems to delete the underlying data in the memory location it points to?

double* ptr_sum = (double*)buf_summary.ptr;

void Pricing::price_all(py::array_t<double>& in_summary){

if (in_summary.ndim() != 2) {
    throw std::runtime_error("Results should be a 2-D Numpy array");
}

py::buffer_info buf_summary = in_summary.request(); // summary

// Pointer definition
double* ptr_sum = (double*)buf_summary.ptr;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文