分类: C/C++
2011-10-31 16:08:24
typedef unsigned short int USHORT; #include int main() { USHORT * pInt = new USHORT; *pInt = 10; std::cout << "*pInt: " << *pInt << std::endl; delete pInt; long * pLong = new long; *pLong = 90000; std::cout << "*pLong: " << *pLong << std::endl; *pInt = 20; // uh oh, this was deleted! 这里的bug引发后面的//*pLong: 65556,前面的bug引发后面未知的威胁 std::cout << "*pInt: " << *pInt << std::endl; //危险 std::cout << "*pLong: " << *pLong << std::endl; //被影响 delete pLong; return 0; } 前面的BUG引发后面未知的错误 typedef unsigned short int USHORT; #include int main() { USHORT * pInt = new USHORT; *pInt = 10; std::cout << "*pInt: " << *pInt << std::endl; delete pInt; //释放内存 pInt = 0; //空指针,后面再*pInt = 20;则程序崩溃 delete pInt; //安全没效果,紧接着两次delete pInt将崩溃 long * pLong = new long; *pLong = 90000; std::cout << "*pLong: " << *pLong << std::endl; // *pInt = 20; // uh oh, this was deleted! *pLong: 65556 //pInt是空指针,将崩溃,后面的得不到执行 // std::cout << "*pInt: "
<< *pInt << std::endl; //pInt是空指针,将崩溃,后面的得不到执行 std::cout << "*pLong: " << *pLong << std::endl; delete pLong; return 0;