我不明白为什么值从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] 删除。
我来说两句