先看一段代码: Java代码 #include <cstddef> #include <cstdio> #include <iostream> using namespace std; class TestSS; class TestStaticMember { public: static TestSS* ssptr; }; TestSS* TestStaticMember::ssptr = NULL; class TestSS{ private: int version; public: TestSS(){ version = 1; cout << "TestSS constructor..." << endl; } ~TestSS(){ cout << "TestSS deconstrcutor..." << endl; //delete version; } void setVersion(const int ver){ version = ver; } int getVersion() const { return version; } }; void init(){ TestSS ss; TestStaticMember::ssptr = &ss; ss.setVersion(111); printf("In init: %p, %p\n", &ss, TestStaticMember::ssptr); cout << "In init: " << TestStaticMember::ssptr->getVersion() << endl; } int main(int argc, char** argv){ init(); printf("In main: %p\n", TestStaticMember::ssptr); cout << "In main: " << TestStaticMember::ssptr->getVersion() << endl; return 0; } #include <cstddef>#include <cstdio>#include <iostream>using namespace std;class TestSS;class TestStaticMember {public: static TestSS* ssptr;};TestSS* TestStaticMember::ssptr = NULL;class TestSS{private: int version;public: TestSS(){ version = 1; cout << "TestSS constructor..." << endl; } ~TestSS(){ cout << "TestSS deconstrcutor..." << endl; //delete version; } void setVersion(const int ver){ version = ver; } int getVersion() const { return version; }};void init(){ TestSS ss; TestStaticMember::ssptr = &ss; ss.setVersion(111); printf("In init: %p, %p\n", &ss, TestStaticMember::ssptr); cout << "In init: " << TestStaticMember::ssptr->getVersion() << endl;}int main(int argc, char** argv){ init(); printf("In main: %p\n", TestStaticMember::ssptr); cout << "In main: " << TestStaticMember::ssptr->getVersion() << endl; return 0;} 在我的环境,Linux 64位,编译器(g++ (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)) 结果输出: TestSS constructor... In init: 0x7fff82029e50, 0x7fff82029e50 In init: 111 TestSS deconstrcutor... In main: 0x7fff82029e50 In main: 111 因为我同事提供给我的一个API与这个类似,且告诉我这样调用,猛一看觉得有问题,于是写了上面的测试代码。但是输出结果出乎我的意料,一时间还以为我对于变量作用域理解有误或者是编译器对static成员做了特殊处理,于是上网搜各种资料,结果是 编译器不保证能正确访问被释放的内存了,即结果是不确定的,如果原来内存里面的值还没被擦除或覆盖就可能读出原来的值。于是疑惑解决了,真实蛋疼。