编辑:根据PlasmaHH的建议增加了带有内存位置的调试输出。cl :: vector vs std :: vector:不同的迭代器行为
我不明白cl :: vector <>在OpenCL的C++绑定中的不同行为。考虑下面的代码:
部首Top.hpp
:
class Top {
public:
void setBool(bool b);
bool getBool();
private:
bool status;
};
源Top.cpp
:
#include "Top.hpp"
void Top::setBool(bool b) {
std::cout << (void*)this << " setBool("<< b<< ")\n";
status = b;
}
bool Top::getBool() {
std::cout << (void*)this << " getBool() returns " << status << std::endl;
return status;
}
使用以上:
#define __NO_STD_VECTOR
#include <iostream>
#include "CL/cl.hpp"
#include "Top.hpp"
using namespace cl;
using namespace std;
cl::vector<Top> js;
int main() {
js.push_back(Top());
js[0].setBool(true);
cout << js[0].getBool() << endl;
for(cl::vector<Top>::iterator i = js.begin(); i != js.end(); ++i) {
(*i).setBool(false);
}
cout << js[0].getBool() << endl;
}
随着__NO_STD_VECTOR
在std ::矢量被覆盖。输出是
0x6021c0 setBool(1)
0x6021c0 getBool() returns 1
0x7fffae671d60 setBool(0)
0x6021c0 getBool() returns 1
所以迭代器返回的位置肯定是错误的。
使用上述与std::vector
(并更改命名空间,当然std
),但是给人的预期输出:
0x1be0010 setBool(1)
0x1be0010 getBool() returns 1
0x1be0010 setBool(0)
0x1be0010 getBool() returns 0
这个迭代器作用不同,但它应该更换的std ::向量避免兼容性问题。我错过了什么吗?
尝试添加类似'std :: cout <<(void *)this <<“setBool(”<< b <<“)\ n”;'到你的setBool函数(类似于getBool)来看看正在进行,以及在哪里设置,如果有的话。这样你可以看到它是否迭代,或者东西是“指向”错误的元素等。 – PlasmaHH 2012-01-31 15:20:19
在任何情况下,'std :: vector'行为都是我所期望的。我无法想象为什么'cl :: vector'会做一些奇怪的事情...... – Xeo 2012-01-31 15:31:16
@Xeo:是的,预计是'std :: vector'。 @PlasmaHH:的确,由迭代器返回的对象具有不同的内存地址,所以它不会返回我的对象并在其他地方设置“false”。 – rdoubleui 2012-01-31 15:34:56