Thread: delete on a NULL pointer

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    2

    delete on a NULL pointer

    Instead of
    Code:
    if (ptr)
    {
      delete ptr;
      ptr = 0;
    }
    can I use
    Code:
     delete ptr;
     ptr = 0;
    Is it true that if delete is called with a null pointer the code will not crash, since delete has internal checks.

    ---------------------------------------
    Why do i want to do this?
    The boss wants me to!

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Why don't you try it and see?

    EDIT: Also, why are you wasting our time? Just out of interest I googled for "c++ delete operator", and the first result was this:
    http://msdn.microsoft.com/library/de...express_29.asp. Have a read.

    Please at least TRY to figure out your problems before you make US do it.
    Last edited by bennyandthejets; 10-02-2003 at 07:37 AM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is it true that if delete is called with a null pointer the code will not crash, since delete has internal checks.

    Yes, it's true, so you can use your second example. See Cat's post here: http://cboard.cprogramming.com/showt...ghlight=delete

  4. #4
    root
    Join Date
    Sep 2003
    Posts
    232
    >Also, why are you wasting our time?
    If it's a waste of your time, do something else. I don't mind directing people to google when they're ignorant of it's usefulness for answering these kind of questions.

    >Please at least TRY to figure out your problems before you make US do it.
    This isn't a problem, it's a simple question concerning the definition of the delete operator. Does it accept a null pointer? Yes. Does passing it a null pointer do anything? No, it's a no-op, so you can write code that assumes either valid new'd memory, or a null pointer and everything will work nicely.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    If myname had googled in the first place, he/she would have gotten the exact answer needed much more quickly, without wasting our time as well.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    root
    Join Date
    Sep 2003
    Posts
    232
    >If myname had googled in the first place
    This is assuming that myname is aware of google and how it can be used to answer questions. Not everyone knows how to find answers to their questions like you do, perhaps you should be more tolerant.

    >without wasting our time as well
    That's a pretty stuck up attitude, dude. I don't know about you, but I come here to help people learn new things. Things like how to use google as well as how to use C/C++.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    In that case, there should be something in the FAQ about how to use Google. I find nearly everything I need using Google, and I wouldn't mind imparting that knowledge to other people. I'll mention it to a moderator.

    Myname, I'm sorry if you didn't know to use Google. Here's a quick tip:

    Go to Google. In the search string, always type c++ first, that way, all your sites should be relevant. then type in the name of whatever it is that you're searching. Eg:

    "c++ delete operator"
    "c++ linked list"
    "c++ class inheritance"
    "c++ dynamic memory allocation"

    A lot of the time, the first result will be an MSDN site. These are great, but remember, never use the search function provided by MSDN. So many times in the past, a search string in MSDN has returned NO results, yet the same search string in Google has returned the perfect page AT MSDN.

    So, next time you have a problem, see if you can find the problem by googling it, and if not, come to us and we'll help.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Twm, I agree with you. Don't you just love these people who are condescending to new posters, even though at one time they were the newbies??? Gee, I'm too lazy to answer your idiotic questions.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I dunno, anyone who can navigate to this site should be able to use *a* search engine like Google, and anyone who can figure how to post a message should be able to use the "search" feature on the board to find the answer.

    I never answer those trivial questions for the reason that if the person isn't going to spend 30 seconds finding the answer out themselves, I am not going to spend 30 seconds typing a reply.

    I have no problem with newbies, I was one once, but I have a problem with people who make no attempt to learn on their own.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'll say this. Cat, until I saw your post about using delete on NULL pointers, I had no idea you could do this.

    And another thing. Have you ever seen Salem answer a question? If you have, you become amazed at how elegant his solutions are. Imagine if experts like him refused to share their knowledge. Sure we'd get an answer, but not one like they provide.

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Swoopy, I don't think Salem could give a better response to this question than Google could.

    I was not condescending to Myname. First I told him/her to try the code to see if it DID make the program crash. It's pretty standard to try something if you're not sure about it. Then I googled it just out of curiousity and found the answer straight away, which implied that myname had not searched for the answer him/herself, as we are told to do in the Board FAQ somewhere I believe. The best way to prevent myname from doing this again was to take a harsh tone. It snaps people into shape when semi-flamed. I then posted the link to the exact answer that myname wanted. Therefore, I did everything necessary: I gave the answer, and I told myname how to get the answer more quickly.

    Twm, I agree with you. Don't you just love these people who are condescending to new posters, even though at one time they were the newbies??? Gee, I'm too lazy to answer your idiotic questions.
    I was once a newbie. You know why I'm not one anymore? Because people like I am now told me the way it is. I was snapped at many a time for asking stupid questions, and now I've learnt my lesson.

    Let's cool this thread down. The question has been answered already. I'm sorry to anyone who doesn't like the way I go about teaching people, but IMO it works.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    BATJ, I agree with the concept you're teaching (do some research before posting).

    The way I think of this CProgramming board is, that it represents the combined knowledge of all who post here, and so each time a question is answered, it help all in this community to become better programmers.

    I know because we have Google, we could rely on google to give us the answer to most of the questions posted. But it's kind of analogous to giving someone the answer via private email. Only one person shares in the knowledge.

    >Swoopy, I don't think Salem could give a better response to this question than Google could.

    This is true. What I meant was, he knows tricks in C/C++ which none of the rest of us know. He could say: "I'm keeping my best triicks to myself." Fortunately he graciously shares them with the rest of the CProgramming community.

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Agreed. Friends again?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Agreed. Friends again?

    Sure, friends again. I'm just glad you, me, and hopefully others took this in good cheer!

  15. #15
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I never answer those trivial questions for the reason that if the person isn't going to spend 30 seconds finding the answer out themselves, I am not going to spend 30 seconds typing a reply.
    He did spend time trying to find the answer, he knew he would get one here, so he asked. I would consider that 'trying to find the answer'. That said, usually before I post I search google and forum history but sometimes it's quicker to just post.

    If it's that painful for any of you, then don't reply, especially replying just to say how useless it would be to reply.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM