Thread: How do "safe delete" for both arrays and non-arrays?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How do "safe delete" for both arrays and non-arrays?

    I've seen numerous functions like:

    Code:
    template <typename T> void SafeDelete(T *&p)
    {
        if ( p )
        {
              delete p;
              p = NULL;
        }
    }
    The check for p is so that you don't delete it twice (which would cause a problem). But if an array gets passed to it, you've got a problem.

    Is there a way to do something *like* this:

    Code:
    template <typename T> void SafeDelete(T *&p)
    {
        if ( p )
        {
              if ( isArray( p ) ) delete [] p;
              else delete p;
              p = NULL;
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The check for null is unnecessary unless you are coding on very very old compilers with bugs, in which case I'd be surprised if the template solution would work anyway. Remove the if ( p ).

    As far as telling whether p was an array or not, I think you'd have to overload the function to take a different type. Although I wonder why you would need this for an array, why wouldn't you be using vector?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Daved View Post
    The check for null is unnecessary unless you are coding on very very old compilers with bugs, in which case I'd be surprised if the template solution would work anyway. Remove the if ( p ).

    As far as telling whether p was an array or not, I think you'd have to overload the function to take a different type. Although I wonder why you would need this for an array, why wouldn't you be using vector?
    I disagree. The following causes an error:

    Code:
    SomeClass* p;
    delete p;
    delete p;
    I can't guarantee that SafeDelete won't be called twice in someone's code on the same pointer. So setting the variable to NULL and then not calling delete again on it is safer.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I said the check for null is unnecessary (the if ( p ) part). I didn't say the setting of the pointer to null was unnecessary.

    I agree that setting the pointer to null can be useful in some cases. In my experience, however, most of the time when you delete a pointer it goes out of scope and is not used anyway.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by 6tr6tr View Post
    I disagree. The following causes an error:

    Code:
    SomeClass* p;
    delete p;
    delete p;
    I can't guarantee that SafeDelete won't be called twice in someone's code on the same pointer. So setting the variable to NULL and then not calling delete again on it is safer.
    Someone could also delete, then SafeDelete. Or pass a pointer that was never new'd. You can't stop all errors this way. The best practice is to use smart pointers and containers instead of raw pointers and arrays. When you must use raw, practice RAII and delete them in the destructor.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's no way to distinguish between array and single object pointers. You simply have to know which it is. 99.99&#37; of the time, that's absolutely no problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The best practice is to use smart pointers and containers instead of raw pointers and arrays. When you must use raw, practice RAII and delete them in the destructor.
    In a way it appears that this SafeDelete is one of the unsafest ways to go about managing memory...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, it's safer than a raw delete
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed