Question

The following code crashes my program:

#include <string>
#include <vector>

class test {
volatile std::vector<std::string> wtf;
public:
    test() {}
    void dope() { wtf.clear(); }
};

int main(){
    (new test())->dope();
    return 0;
}

And I have no idea why. When I remove volatile, it works again. So why is volatile a problem ?

Was it helpful?

Solution

std::vector::clear() doesn't have volatile qualifier.

So calling it with a volatile vector is illegal.

BTW, volatile is not a magic keyword for multi-threading.
You may use mutex to protect access to your vector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top