【发布时间】:2014-02-18 09:59:30
【问题描述】:
以下代码使我的程序崩溃:
#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;
}
我也不知道为什么。当我删除 volatile 时,它会再次起作用。那么为什么 volatile 是个问题呢?
【问题讨论】:
-
我认为真正的问题是你为什么想要
volatilestd::vector? -
我得到了(使用 gcc 4.8.1):
error: passing 'volatile std::vector<std::basic_string<char> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::clear() [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]' discards qualifiers [-fpermissive] -
它包含程序每次都必须访问的信息。而且它是多线程的。
-
为什么是
volatile?std::vector是volatile是没有意义的。 -
Volatile 不会像你想象的那样做。