我在我的项目中使用stl容器,我发现了一个我无法解释的奇怪错误。我们考虑以下代码:
#include <iostream>
#include <vector>
int main(int argc, char** argv)
{
std::vector<bool> vec;
vec.resize(5, false);
std::cout << vec.at(0);
}
这会按预期输出0,但如果我使用drmemory运行内存检查,则会发现未初始化的读取。任何人都可以帮助理解这种行为吗?
平台:win32; 编译器:mingw32 - gcc 4.7.2; Drmemory 1.6.0 - build 2
答案 0 :(得分:8)
std::vector<bool>
是一个奇怪的小东西,使用bit twiddling来实现其目标。在这种情况下,我会满足于建议你所看到的只是一个红色的鲱鱼。
话虽如此,you might be better off with some other container,因为this template specialisation is universally despised。