打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delete p 究竟把p怎么了?(某公司校园招聘笔试试题)
userphoto

2015.12.27

关注

     delete p究竟把p怎么了?是删除p吗?是改变p的指向吗?一切答案尽在下面的程序中:(C中的free与C++中的delete类似.)

 

  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int *p = new int;  
  6.     cout << "random *p: " <<*p << endl;  
  7.       
  8.     *p = 10;  
  9.     cout << "the address of p: " << &p << endl;  
  10.     cout << "the value of p: " << p << endl;  
  11.     cout << "the value of *p : " << *p << endl;  
  12.     delete p;  
  13.       
  14.     cout << endl;  
  15.       
  16.     cout << "---After executing (delete p;)---" << endl;  
  17.     cout << endl;  
  18.     cout << "the address of p: " << &p << endl;  
  19.     cout << "the value of p: " << p << endl;  
  20.     cout << "the value of *p: " << *p << endl << endl;  
  21.       
  22.     return 0;  
  23. }  

结果为:

random *p: -842150451
the address of p: 0012FF7C
the value of p: 00381F28
the value of *p : 10

---After executing (delete p;)---

the address of p: 0012FF7C
the value of p: 00381F28
the value of *p: -572662307


  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一道题看指针和数组的工作原理
简单的输入输出
93 f0312
C++使用二级指针、三级指针给函数传参
c++ cin输入字符串
闯关游戏C 代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服