Why doesn't delete reset its pointer to NULL?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 4
Reputation: kbmmartin is an unknown quantity at this point 
Solved Threads: 0
kbmmartin kbmmartin is offline Offline
Newbie Poster

Why doesn't delete reset its pointer to NULL?

 
0
  #1
Apr 24th, 2009
After this code executes
  1. ptr = new myStuff;
  2. // some code ...
  3. delete ptr;

the memory allocated is freed but ptr still contains the mem addr of the 1st byte of the previously allocated mem. Is there some history to this? Why didn't the C++ standard also have the delete operator reset ptr to NULL? Is there some clever coding which makes use of ptr still pointing to the freed mem?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #2
Apr 24th, 2009
Yeah, I think the clever code is called a runtime error. I'm no expert though.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #3
Apr 24th, 2009
Usually people that code in C++ don't want things done for them that they don't have control over. If you really want something that nulls and deletes a pointer, write it yourself.

As for a reason, this is a bit contrived but...what if you where writing an operating system that runs a thread to delete junk pointers (like the garbage collector in .NET). Now what if you wanted to keep a list of these pointers to send to a different thread at a certain interval to indicate memory that is now usable? I suppose you could create the list before the pointer is deleted, but then you are just taking up extra memory!
Last edited by skatamatic; Apr 24th, 2009 at 8:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #4
Apr 25th, 2009
After deleting you could simply set your pointer manually to NULL:
  1. int *ptr = new int;
  2. delete ptr;
  3. ptr = 0;
By the way, why should delete reset the pointer to NULL , C++ is a programmer's language and those programmers want to control every single aspect of the language, this brings with it that you'll have to do some things manually, but in this case, it's only one single line of code extra ...
Last edited by tux4life; Apr 25th, 2009 at 11:56 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #5
Apr 25th, 2009
>>Why didn't the C++ standard also have the delete operator reset ptr to NULL?
First of all you are simply **NO ONE** to ask question like this. C++ standards have been made by the most proficient and experienced programmers of this language. If you think there should be a feature added doesn't means it should be added. A boon to you may be disaster to others. C++ standards have to keeps everyone in mind.

If you really feel that everyone is dumb and delete should really reset the pointer to null answer this:
Lets say I have the following code. Just check what would happened if delete would reset the pointer to zero:
  1. int * p=new int;
  2. int *q=p;// q points to same memory as p
  3. delete p; //deleting p and setting it to zero
  4. delete q;// Shit ! deleting pointer to null 0
Got that!!
So before opening the mouth and say "Mommy I want this candy" try to think how costly( or perhaps dangerous) that candy would be. Even if you can't think how costly it is, just trust your mother that she did a good thing by not buying you that candy.
Last edited by siddhant3s; Apr 25th, 2009 at 11:58 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #6
Apr 25th, 2009
Yeah, if you really want a feature like that you might have to implement it yourself: Create a new pointer datatype and overload the delete operator for it in such a way that the pointer is always set to NULL after releasing the allocated memory ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #7
Apr 25th, 2009
Originally Posted by tux4life View Post
Yeah, if you really want a feature like that you might have to implement it yourself: Create a new pointer datatype and overload the delete operator for it in such a way that the pointer is always set to NULL after releasing the allocated memory ...
If really wanted to things like this, I would simply create a (templatized) function like this
  1. template<class T>
  2. inline void destroy(T*& p)
  3. {
  4. delete p;
  5. p = 0;
  6. }

But the fact remains that these things( my function hack or your overloaded pointer hack or any other such things) will lead to error like I pointed.
Also note what happens when I use this destroy function like this:
  1. int* f();//some function returning a pointer.
  2. int* p;//some pointer pointing to some memory.
  3. // ...
  4. destroy(f()); // error: trying to pass an rvalue by non-const reference
  5. destroy(p+1); // error: trying to pass an rvalue by non-const reference
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #8
Apr 25th, 2009
Originally Posted by siddhant3s View Post
>>Why didn't the C++ standard also have the delete operator reset ptr to NULL?
First of all you are simply **NO ONE** to ask question like this. C++ standards have been made by the most proficient and experienced programmers of this language. If you think there should be a feature added doesn't means it should be added. A boon to you may be disaster to others. C++ standards have to keeps everyone in mind.

If you really feel that everyone is dumb and delete should really reset the pointer to null answer this:
Lets say I have the following code. Just check what would happened if delete would reset the pointer to zero:
  1. int * p=new int;
  2. int *q=p;// q points to same memory as p
  3. delete p; //deleting p and setting it to zero
  4. delete q;// Shit ! deleting pointer to null 0
Got that!!
So before opening the mouth and say "Mommy I want this candy" try to think how costly( or perhaps dangerous) that candy would be. Even if you can't think how costly it is, just trust your mother that she did a good thing by not buying you that candy.
Seems a bit hard on the OP. He/she was just asking to be educated. Is that a problem?
As for your code, I hope the OP does get it, but I'm afraid I don't.
Calling delete on p deallocates it and, at the request of the OP, sets P to NULL. How does that affect q? q still contains a pointer to the memory which has been deallocated. Calling delete q after delete p is always doomed to failure whether p was cleared or not.
Not trying to be difficult. I genuinely don't see what you're trying to illustrate here.
Please educate me - but be nice eh...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Why doesn't delete reset its pointer to NULL?

 
1
  #9
Apr 25th, 2009
sid> Just check what would happened if delete would reset the pointer to zero:

Nothing would happen! Are you a mindless idiot? Did your brain fall out? Did you replace it with a turd you found on the ground? delete does nothing if it's passed a zero pointer! Moron!

I mean, how stupid would you have to be to think that the OP's question was about the designers of C++ being idiots? Answer: a complete and total moron, i.e., siddhantes!!! The OP's question was about the REASONING (something sid cannot comprehend) of the designers.

To the OP: Don't worry about the "sidiot," as we shall call him. He has serious personality problems. Just ignore anything he says. Everybody else does!

(P.S. The tone of the above is entirely contingent on the sidiot's original tone.)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Why doesn't delete reset its pointer to NULL?

 
0
  #10
Apr 25th, 2009
>>Seems a bit hard on the OP. He/she was just asking to be educated. Is that a problem?
Not at all. I have done my job.

>>
As for your code, I hope the OP does get it, but I'm afraid I don't.
Calling delete on p deallocates it and, at the request of the OP, sets P to NULL. How does that affect q? q still contains a pointer to the memory which has been deallocated. Calling delete q after delete p is always doomed to failure whether p was cleared or not.
<<
According to the standards, the behavior is undefined (that means the consequence can be from a simple error to the crash of the machine) and is implementation dependent.

>>
Not trying to be difficult. I genuinely don't see what you're trying to illustrate
here.
<<
You would if you wanted ( and not trying to be over-sympathetic ). I am trying to satisfy OP that this behavior of delete has a reason. But on the same time I am suggesting the OP, not to ask such questions relating to (why the hell this )standards ( is applied) . ( If you have a doubt and you feel that there is an error in the standard, bring a solution out)

>>
Please educate me - but be nice eh...
<<
You mean polite? Well sure, but not in the next 3 hrs atleast.
If you want more reference try getting something on Bjarne Stroustrup homepage :
http://www.research.att.com/~bs/bs_f...ml#delete-zero
Last edited by siddhant3s; Apr 25th, 2009 at 2:07 pm.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC