c++stldr-memory

C++ std::vector<bool> gives uninitialized read error using drmemory


I'm using stl containers in my project and I discovered a weird error that I can't explain. Let's consider the following code:

#include <iostream>
#include <vector>

int main(int argc, char** argv)
{
    std::vector<bool> vec;
    vec.resize(5, false);
    std::cout << vec.at(0);
}

This outputs 0 as expected, but if I run a memory check with drmemory it discovers an uninitialized read. Can anybody help in understanding this behaviour?

Platform: win32 ; Compiler: mingw32 - gcc 4.7.2 ; Drmemory 1.6.0 - build 2


Solution

  • std::vector<bool> is a bizarre little thing, using bit twiddling to achieve its goals. I'd be content in this instance to suggest that what you're seeing is just a red herring.

    That being said, you might be better off with some other container, because this template specialisation is universally despised.