473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Does "delete" work?

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
4 1926
Meetee
931 Expert Mod 512MB
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
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
931 Expert Mod 512MB
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
3,652 Expert 2GB
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

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

Similar topics

4
by: Stefan Strasser | last post by:
why is delete an expression and not a statement? (in my draft copy of the standard). I was about to ask the same question about "throw" but found an expression case of throw("return boolvalue ? 5...
6
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
1
by: Michael Ramey | last post by:
Hi, I'm dynamically creating a table of "delete" imagebuttons, that correspond to files on the webserver. I want to respond to clicks of these buttons, so I know to know what file to delete. ...
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
1
by: D. Susman | last post by:
Hi, When I call "clean" as shown below, shouldn't I be getting segmentation fault due to garbage data? I am compiling with CC and the output is 5. Does "delete" not delete in this situation or...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.