delete vs NULL vs free in c++

后端 未结 6 944
谎友^
谎友^ 2020-12-13 04:16

what is the difference between deleting a pointer, setting it to null, and freeing it.

delete ptr;

vs.

ptr=NULL;


        
相关标签:
6条回答
  • 2020-12-13 04:29

    Your question suggests that you come from a language that has garbage collection. C++ does not have garbage collection.

    If you set a pointer to NULL, this does not cause the memory to return to the pool of available memory. If no other pointers point to this block of memory, you now simply have an "orphaned" block of memory that remains allocated but is now unreachable -- a leak. Leaks only cause a program to crash if they build up to a point where no memory is left to allocate.

    There's also the converse situation, where you delete a block of memory using a pointer, and later try to access that memory as though it was still allocated. This is possible because calling delete on a pointer does not set the pointer to NULL -- it still points to the address of memory that previously was allocated. A pointer to memory that is no longer allocated is called a dangling pointer and accessing it will usually cause strange program behaviour and crashes, since its contents are probably not what you expect -- that piece of memory may have since been reallocated for some other purpose.

    [EDIT] As stinky472 mentions, another difference between delete and free() is that only the former calls the object's destructor. (Remember that you must call delete on an object allocated with new, and free() for memory allocated with malloc() -- they can't be mixed.) In C++, it's always best to use static allocation if possible, but if not, then prefer new to malloc().

    0 讨论(0)
  • 2020-12-13 04:29

    As with any indirection, there are two objects involved when using pointers: the referrer (the pointer, in your example ptr) and the referenced object (what it points to, *ptr). You need to learn to differentiate between them.

    When you assign NULL to a pointer, only the pointer is affected, the object it refers to is left alone. If the pointer was the last one pointing to that object, you have lost the last referring pointer pointing to it and thus cannot use it anymore. In C++, however, that does not mean the object will be deleted. C++ does not have a garbage collection. So the object is leaked.

    In order for objects to be deleted, you will have to delete them manually by passing their address (stored in a pointer) to the delete operator. If you do this, only the object referred to will be affected, the pointer is left alone. It might still point to the address where the object resided in memory, even though that isn't usable anymore. That is called a dangling pointer.

    0 讨论(0)
  • 2020-12-13 04:35
    int * ptr = null;
    

    Calling free(ptr) will do nothing. According to standard if given pointer is null then no action happens.

    Using delete p will also do nothing. C++ standard says that no action will happen because it points to nothing and no type is available.

    0 讨论(0)
  • 2020-12-13 04:37

    delete will give allocated memory back to the C++ runtime library. You always need a matching new, which allocated the memory on the heap before. NULL is something completely different. A "placeholder" to signify that it points to no address. In C++, NULLis a MACRO defined as 0. So if don't like MACROS, using 0 directly is also possible. In C++0x nullptr is introduced and preferable.

    Example:

    int* a;        //declare pointer
    a = NULL;      //point 'a' to NULL to show that pointer is not yet initialized 
    a = new int;   //reserve memory on the heap for int
    
    //... do more stuff with 'a' and the memory it points to
    
    delete a;      //release memory
    a = NULL;      //(depending on your program), you now want to set the pointer back to
                   // 'NULL' to show, that a is not pointing to anything anymore
    
    0 讨论(0)
  • 2020-12-13 04:42

    When you create an object using new, you need to use delete to release its memory back to the system. The memory will then be available for others to reuse.

    
    int* a = new int;
    delete a;
    

    NULL is just a pre-defined macro that can be assigned a pointer to mean that it does not point to anything.

    
    int* a = new int;
    a = NULL;
    

    In the above case, after allocating storage for a, you are assigning NULL to it. However, the memory previously allocated for a was not released and cannot be reused by the system. This is what we call memory leak.

    0 讨论(0)
  • 2020-12-13 04:44

    well, if you created object dynamically(with 'new'), setting pointer to any value does not delete object from memory - and you get a memory leak.

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