Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

dr-memory

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

like image 616
Cactusjoe Avatar asked Oct 14 '13 11:10

Cactusjoe


1 Answers

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.

like image 173
Lightness Races in Orbit Avatar answered Oct 20 '22 22:10

Lightness Races in Orbit