Reason why not to have a DELETE macro for C++

前端 未结 12 985
臣服心动
臣服心动 2020-11-29 07:01

Are there any good reasons (except \"macros are evil\", maybe) NOT to use the following macros ?

#define DELETE( ptr ) \\
i         


        
相关标签:
12条回答
  • 2020-11-29 07:09
    1. Macros are evil. Why not use inline templated functions?
    2. You can delete null ptrs.
    3. In many cases you don't need to set the ptr to null - destructors for instance.
    0 讨论(0)
  • 2020-11-29 07:10

    Personally I prefer the following

    template< class T > void SafeDelete( T*& pVal )
    {
        delete pVal;
        pVal = NULL;
    }
    
    template< class T > void SafeDeleteArray( T*& pVal )
    {
        delete[] pVal;
        pVal = NULL;
    }
    

    They compile down to EXACTLY the same code in the end.

    There may be some odd way you can break the #define system but, personally (And this is probably going to get me groaned ;) I don't think its much of a problem.

    0 讨论(0)
  • 2020-11-29 07:11
    1. deleting a null pointer does nothing, so no need to check whether the pointer is null before deletion. Nullifying the deleted pointer might still be needed (but not in every case).

    2. Macros should be avoided as much as possible, because they are hard to debug, maintain, introduce possible side effects, they are not part of namespaces, etc.

    3. deleting a pointer that was not dynamically allocated with new will still be a problem...

    0 讨论(0)
  • 2020-11-29 07:11

    Yes, you should never call delete directly. Use shared_ptr,scoped_ptr,unique_ptr or whatever smart pointer you have in your project.

    0 讨论(0)
  • 2020-11-29 07:14

    Because it is OK to delete a NULL(0) pointer. The is no need to check if the pointer actually is NULL(0) or not. If you want to set the pointer to NULL, after deleting, then you can overload the delete operator globally with out using macros.


    It seems that I was wrong about the second point:

    If you want to set the pointer to NULL, after deleting, then you can overload the delete operator globally

    The thing is that if you overload the global new and delete, you could have something like this:

    void* operator new(size_t size)
    {
        void* ptr = malloc(size);
    
        if(ptr != 0)
        {
            return ptr;
        }
    
        throw std::bad_alloc("Sorry, the allocation didn't go well!");
    }
    
    void operator delete(void* p)
    {
        free(p);
        p = 0;
    }
    

    Now, if you set p = 0; in the overloaded delete, you are actually setting the local one, but not the original p. Basically, we are getting a copy of the pointer in the overloaded delete.

    Sorry, it was on the top of my head, I gave it a second thought now. Anyway, I would write template inline function to do the thing instead of writing EVIL MACROS :)

    0 讨论(0)
  • 2020-11-29 07:15

    Use boost::shared_ptr<> instead.

    http://www.boost.org/doc/libs/1_39_0/libs/smart_ptr/shared_ptr.htm

    The MACRO here provides some of the functionality you are probably looking for.

    0 讨论(0)
提交回复
热议问题