編輯:根據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