943,589 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4952
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 24th, 2009
0

Why doesn't delete reset its pointer to NULL?

Expand Post »
After this code executes
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kbmmartin is offline Offline
4 posts
since Apr 2009
Apr 24th, 2009
0

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

Yeah, I think the clever code is called a runtime error. I'm no expert though.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 24th, 2009
0

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

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.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Apr 25th, 2009
0

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

After deleting you could simply set your pointer manually to NULL:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
-2

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

>>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:
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 25th, 2009
0

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

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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by tux4life ...
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
cpp Syntax (Toggle Plain Text)
  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:
cpp Syntax (Toggle Plain Text)
  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
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 25th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
>>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:
cpp Syntax (Toggle Plain Text)
  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...
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Apr 25th, 2009
1

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

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.)
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 25th, 2009
0

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

>>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: glibc and seg fault
Next Thread in C++ Forum Timeline: C++ Random Number Generator Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC