1,105,375 Community Members

Why doesn't delete reset its pointer to NULL?

Member Avatar
kbmmartin
Newbie Poster
4 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

After this code executes

ptr = new myStuff;
// some code ...
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?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Yeah, I think the clever code is called a runtime error. I'm no expert though.

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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!

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

After deleting you could simply set your pointer manually to NULL:

int *ptr = new int;
delete ptr;
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 :) ...

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
-3
 

>>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:

int * p=new int;
int *q=p;// q points to same memory as p
delete p; //deleting p and setting it to zero
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.

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

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 :) ...

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
0
 

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

template<class T> 
inline void destroy(T*& p) 
{ 
    delete p; 
    p = 0; 
}

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:

int* f();//some function returning a pointer.
	int* p;//some pointer pointing to some memory.
	// ...
	destroy(f());	// error: trying to pass an rvalue by non-const reference
	destroy(p+1);	// error: trying to pass an rvalue by non-const reference
Member Avatar
MrSpigot
Junior Poster
156 posts since Mar 2009
Reputation Points: 64 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 0 [?]
 
0
 

>>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:

int * p=new int;
int *q=p;// q points to same memory as p
delete p; //deleting p and setting it to zero
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... :S

Member Avatar
nucleon
Posting Pro in Training
476 posts since Oct 2008
Reputation Points: 114 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 0 [?]
 
2
 

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.)

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
0
 

>>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_faq2.html#delete-zero

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

I hope the OP does get it, but I'm afraid I don't.

Didn't you mean: but I'm afraid he doesn't ?

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
0
 

>>Nothing would happen!
Just check on your part. You mean if compiler doesn't complain, everything is OK?
These are few of the undefined behaviors which means ( Effective C++ by Mayers gives a good info about Undefined behaviors) "anything can happen".
Or in other words it is implementation dependent.

>>! Are you a mindless idiot? Did your brain fall out? Did you replace it with a turd you found on the ground?
Answer to all these (useless) question is NO.

>>Answer: a complete and total moron, i.e., siddhantes!!! The OP's question was about the REASONING (something sid cannot comprehend) of the designers.

You are no one to judge my comprehension.

>>Don't worry about the "sidiot," as we shall call him. He has serious personality problems. Just ignore anything he says.
That was not funny. It was not in the same zipcode, funny.
I have a lot of other problems rather than dealing with my personality. Specially, I hate when people speak without having adequate knowledge.

>> Everybody else does!
Give me at least 5 names plz.

>>(P.S. The tone of the above is entirely contingent on the sidiot's original tone.)
May be, but my post was not as useless your is.

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

Yeah, siddhantes is right (look at the Bjarne Stroustrup's page) !
It isn't because it seems to work it's actually working (that's just C++) !

Member Avatar
MrSpigot
Junior Poster
156 posts since Mar 2009
Reputation Points: 64 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 0 [?]
 
0
 

Useful link, thanks.
So, for the benefit of anyone else who didn't get that code, the point is that resetting p to NULL can be pointless since it is easily circumvented by copying the pointer.
Also interesting to see in that link that Bjarne says the standard doesn't prevent compilers from doing the reset (for lvalues) and he had hoped that more implementations would take this approach.

tux4life - does this pass the grammar check ;)

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

tux4life - does this pass the grammar check ;)

I don't know as I'm not a "native speaker" :P ...

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
0
 

Useful link, thanks.
So, for the benefit of anyone else who didn't get that code, the point is that resetting p to NULL can be pointless since it is easily circumvented by copying the pointer.
Also interesting to see in that link that Bjarne says the standard doesn't prevent compilers from doing the reset (for lvalues) and he had hoped that more implementations would take this approach.

Good to know now your getting on it. Phew.
Note that even though Stroustrup is father of C++, his job is not to write compilers. He may hope that implementors will do it someday but you may fail to think that it is not an easy job for the implementors
For the time being, just create your own function.

To summarize:
*Deleting a pointer to null is undefined behavior. But it is a bad bad practice.
[edit] Sorry it is a defined behavior. deleting a pointer to null won't do anything to the pointer. C++ guarantees this. http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8
[/edit]
*Deleting a pointer twice is an udefined behaviour too.(though you may argue me that it gives you an error on your compiler) and is implementation dependent. Not to mention it is too a bad practice.
*Reseting to Null by delete is certainly not a good idea.
*(Sply to nucleon) Trying to be a hero(whose knowledge is doubtable) is certainly not a good idea.
*Being sympathetic to OP may be nice, but it solves nothing
*It doesn't really mater to OP how hard we fight, he will always win ;)

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

*Deleting a pointer twice is an udefined behaviour too.

Now is just the question left: To prevent deleting a pointer twice: how do we know a pointer has already been deleted :P ?
(I would solve that by implementing a new pointer type, but what's your vision on this?)

Member Avatar
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,429 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 11 [?]
 
0
 

>>*Deleting a pointer to null is undefined behavior. But it is a bad bad practice.[edit] Sorry it is a defined behavior. deleting a pointer to null won't do anything to the pointer. C++ guarantees this. http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8
[/edit]

Member Avatar
William Hemsworth
Posting Virtuoso
1,546 posts since Mar 2008
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 158 [?]
Skill Endorsements: 10 [?]
 
0
 

This thread is out of control.

To the OP: C++ was designed to allow the programmer to have nearly full control over what's happening in the program. Automatically assigning a pointer to NULL once it's been deleted is a waste of cpu, as once deleted, it's usually not used again until it's been reassigned.

Besides causing many other problems, here is an obvious reason not to.

char *buffer = new char[100];
delete[] buffer;
buffer = new char[50];

Would turn to:

char *buffer = new char[100];
delete[] buffer;
[B]buffer = NULL;[/B] // What's the point in this line
buffer = new char[50];

Or say that you have two dynamic objects which have both been deleted, but you still want to compare the original address to see if they were the same object. You would be unable to do so as all deleted objects point to NULL instead of their original adresses. The point is, the address can still be considered useful even after the data has been freed.

To siddhant3s: There's a difference bettween being rude and helpful. The OP asked a simple question which he learned from, so would it hurt to just be nice? :icon_rolleyes:

Member Avatar
kbmmartin
Newbie Poster
4 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Siddhant3s: Wow, some response! You need to lay off the caffeine for a while.

1. I'm not demanding. I'm asking a history question -- why was this decision made? The answer may give me a better insight into the language or a neat new trick to use.
2. Your p = q; delete p; delete q example is bogus. The delete operator accepts a NULL pointer w/ no problem. (You do get a problem if you use a non-zero constant.)

By the way, I've programmed for over 30 years at HP and TI. I'm a part-time prof at a local university and I couldn't answer this question asked by a student.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article