分享

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

 torony 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


  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约