Collectives on Stack Overflow. Learn more. Why doesn't delete set the pointer to NULL? Ask Question. Asked 12 years, 9 months ago. Active 4 years, 4 months ago. Viewed 57k times. But having said that I can think of couple of reasons why the standard would have restricted this: Performance: An additional instruction could slow down the delete performance. Could it be because of const pointers. Then again standard could have done something for this special case I guess.
Does anyone know exact reasons for not allowing this? Improve this question. Sam 7, 15 15 gold badges 44 44 silver badges 65 65 bronze badges. Add a comment. Active Oldest Votes. But the main issue he raises is that delete's argument need not be an lvalue. Improve this answer. XP1 6, 7 7 gold badges 49 49 silver badges 60 60 bronze badges. Dan Olson Dan Olson I think this could use some more explanation. I'm not even sure what he's saying I suppose I'll have to come back at a later time when I can dedicate a couple hrs to researching this until I get it.
Or, you could expound upon the answer to help us understand more quickly. Good point wrt calculated address, even if it's something you don't see often — snemarch. Patryk You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email. Skip to content This is an often asked question in newsgroups and forums.
A lot of people do this :- if p delete p; The assumption is that, if they do not check for NULL , delete will result in a crash or in random behavior. Share this: Twitter Facebook. Accept Solution Reject Solution. You cannot 'delete' a null pointer. In any case, free on a null pointer does nothing. Posted Dec am CPallini.
A null pointer indicates that there is "nothing there" - it points at nothing. So you can't delete or free it, because you didn't assign it. Most likely, this is an error message you are getting as a result of a problem in your code - you are trying to use a pointer that you have not assigned an address into, so the system is reporting a "null pointer" error. Copy Code. Posted Dec am OriginalGriff. Add your solution here. OK Paste as. Treat my content as plain text, not as HTML.
Existing Members Sign in to your account. This email is in use. Accept Solution Reject Solution. You cannot 'delete' a null pointer. In any case, free on a null pointer does nothing.
Posted Dec am CPallini. A null pointer indicates that there is "nothing there" - it points at nothing. So you can't delete or free it, because you didn't assign it.
Most likely, this is an error message you are getting as a result of a problem in your code - you are trying to use a pointer that you have not assigned an address into, so the system is reporting a "null pointer" error.
Copy Code. Posted Dec am OriginalGriff. Add your solution here. OK Paste as. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use.
0コメント