Search code examples
c++crashvolatile

volatile member can not be accessed by member function


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 ?


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.