By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,910 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,910 IT Pros & Developers. It's quick & easy.

Does "delete" work?

P: 5
The below is my code and my compiler is vc++:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6.    int *pint = new int(1024);
  7.  
  8.    cout << "The initialized value of *pint is " << *pint << "\n";
  9.  
  10.    delete pint;
  11.  
  12.    cout << "The deleted *pint is " << *pint << "\n";  
  13.  
  14.    return 0;
  15.  
  16. }
  17.  
The result is below:

The initialized value of *pint is 1024
The deleted *pint is 3604872

does it mean "delete" doesn't work?
Sep 20 '07 #1
Share this Question
Share on Google+
4 Replies


Meetee
Expert Mod 100+
P: 931
The below is my code and my compiler is vc++:
/************************************************** */
#include <iostream>
using namespace std;

int main(){

int *pint = new int(1024);

cout << "The initialized value of *pint is " << *pint << "\n";

delete pint;

cout << "The deleted *pint is " << *pint << "\n";

return 0;

}
/************************************************** *****/
The result is below:

The initialized value of *pint is 1024
The deleted *pint is 3604872

does it mean "delete" doesn't work?
You have already deleted *pint so after delete pint; there is no 1024 value in *pint. So it is giving diffrent value.

Regards
Sep 20 '07 #2

P: 5
I think so too until I do another test
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6.   int val = 1024;
  7.  
  8.   int *pia = &val;
  9.  
  10.   delete pia;
  11.  
  12.   cout << "deleted value of *pia is " << *pia << "\n"; 
  13.  
  14.   return 0;
  15. }
  16.  
  17.  
And the result is 1024.
Why???
Sep 20 '07 #3

Meetee
Expert Mod 100+
P: 931
I think so too until I do another test
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6.   int val = 1024;
  7.  
  8.   int *pia = &val;
  9.  
  10.   delete pia;
  11.  
  12.   cout << "deleted value of *pia is " << *pia << "\n"; 
  13.  
  14.   return 0;
  15. }
  16.  
  17.  
And the result is 1024.
Why???
I don't understand how did you get this output. I tried the above code on both linux and windows. It's giving errornous output as delete will always delete allocated memory and here int *pia = &val; doesn't allocate memory. Instead you can try
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int val = 1024;
  5.  
  6. int *pia = new int(val);
  7.  
  8. delete pia;
  9. cout << "deleted value of *pia is " << *pia << "\n";
  10.  
  11. return 0;
  12. }
  13.  
And it will give the previous code's output!

Regards
Sep 20 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
Once you delete a pointer, you have no guarantee that it will point to valid information anymore. As soon as it was deleted, the memory location it pointed to might have been claimed by some other process on your computer. When you try and use a pointer after it has been deleted, you are invoking *everybody's favorite catchphrase) undefined behavior, so no one knows what will happen every time.
Sep 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.