函数之间的数组指针中的值丢失(与swig编译以在python3中使用)

哎呀

我不明白为什么值从func1丢失到func2然后又丢失了。在func1中它可以正常打印,但在func2和main中却失败。我不认为这是一个棘手的问题,更像是c ++代码问题〜您可以使用以下代码重现该问题。

我的test.cpp:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <mutex>
#include "test.h"
void test::func1(float* feat) {
    std::vector<float> fv = {1,2,3,4,5,6,7};
    feat = fv.data();
    for (std::size_t i = 0; i < 7; ++i){
    std::cout <<  *feat << std::endl;
    feat++;
    }
}


bool test::func2(float* feat) {
    test::func1(feat);
}
bool test::main(float* feat){
    test::func2(feat);
    for (std::size_t i = 0; i < 7; ++i){
    std::cout <<  *feat << std::endl;
    feat++;
    }
}

我的test.h:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <mutex>

class test {
public:
    void func1(float* feat);
    bool func2(float* feat);
    bool main(float* feat);
};

我的测试

%module test
%{
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}

%include "carrays.i"
%array_functions(float, floatArray); 

%include <std_string.i>
%include "test.h"

当我在python3中测试时:

>>> from test import test, new_floatArray, floatArray_getitem
>>> import numpy as np
>>> pp = test()
>>> temp = new_floatArray(5)
>>> pp.main(temp)
1
2
3
4
5
6
7
0
0
0
0
0
4.02252e-14
1.4013e-44
False
坦率
feat = fv.data();

该行不会更改feat指向的数据,它会更改feat指向本地版本的数据

因此,当您从返回时func2()feat指向的数据不会更改。由于您将单元化数据传递给main,因此您可以从func 1(从其自己的数据)中获得7个打印内容,然后是未初始化数据的7个打印内容。

我怀疑你的意思是:

memcpy(feat, fv.data(), fv.size() * sizeof(float));

这会将数据从复制fv到数据feat点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

数组类指针在main中丢失了值

函数中的指针数组未显示在调用中使用的数组的正确值上

如何在两个不同的进程之间通过python3中的指针地址共享数组?

在Python3中使用单元测试和正常进行测试之间的区别

Python3。在值字典中查找数组的值

如何在python3中使用python2 input()函数?

如何使用for循环在python3中使用不同的预处理函数?

为什么在调用函数中使用指针破解以在C中查找数组大小时给出不同的值

在python3中使用多个函数编写xml元素

在python3中使用字典调用带有参数的完整函数

如何在python3中使用数组join()方法添加数字?

python - 如何在Python3中使用键和值数组作为二维列表从用户那里获取字典?

在 Python3 中使用 filter() 從字典值列表中刪除列表

在C ++ 11中使用函数指针

C中的struct中使用的函数指针

在Python中重现C函数指针数组

丢失数组指针值?

函数在Python中返回指针而不是值

如何在python3中使用Asterisk AGI?

在python3中使用readline自动完成

在Python3中使用Selenium抓取动态表

在Python3中使用%x格式是否不好?

在Python3中使用循环计算阶乘

在Python3中使用Script = argv

在Python3中使用imshow更新图例条目

尝试在python3中使用全局字典

在python3中使用tkinter的子帧

在Python3中使用“不在”的速度快于在Python3中使用“在”的速度吗?

函数执行后结构数组中的值丢失