Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480579
  • 博文数量: 98
  • 博客积分: 3265
  • 博客等级: 中校
  • 技术积分: 1227
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 00:29
文章分类

全部博文(98)

文章存档

2012年(6)

2011年(83)

2010年(9)

分类: 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;

}

阅读(1872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~