Is It Good Practice to Null a Pointer After Deleting It

Is it good practice to NULL a pointer after deleting it?

Setting a pointer to 0 (which is "null" in standard C++, the NULL define from C is somewhat different) avoids crashes on double deletes.

Consider the following:

Foo* foo = 0; // Sets the pointer to 0 (C++ NULL)
delete foo; // Won't do anything

Whereas:

Foo* foo = new Foo();
delete foo; // Deletes the object
delete foo; // Undefined behavior

In other words, if you don't set deleted pointers to 0, you will get into trouble if you're doing double deletes. An argument against setting pointers to 0 after delete would be that doing so just masks double delete bugs and leaves them unhandled.

It's best to not have double delete bugs, obviously, but depending on ownership semantics and object lifecycles, this can be hard to achieve in practice. I prefer a masked double delete bug over UB.

Finally, a sidenote regarding managing object allocation, I suggest you take a look at std::unique_ptr for strict/singular ownership, std::shared_ptr for shared ownership, or another smart pointer implementation, depending on your needs.

Why is it recommended to set a pointer to null after deleting it?

Just so that you know the pointer does not point to anything anymore, and will fail if conditions and other boolean checks:

   delete ptr;
ptr = NULL;
if(ptr)
*ptr = 2;

This code will run perfectly fine, although it would cause memory corruption if the pointer was not set to NULL.

C++ - Why set object to null after deleting?

It's unnecessary. Some people make a habit of doing this even when it has no result. An aggressive compiler optimizer will eliminate this code, so it doesn't actually do any harm. But I would have written:

void DeleteAfter(Node *head) {
if (head) {
Node *next = head->next;
if (next) {
head->next = next->next;
delete next;
}
}
}

Note I eliminated a useless level of indirection and added a check to make sure there's a "node after" to delete.

The rationale for the habit is that if a pointer always refers to either a valid object or is null, you can rely on null checks as equivalent to validity checks.

For this reason, Ada, a language often used in safety critical systems, initializes pointers to null and defines its delete equivalent operator to set its argument null automatically. Your C++ is simulating this behavior.

In practice, the value of this discipline is not what you'd hope. Once in a long while it prevents a silly error. One nice thing, however, is that debugger displays of pointer content make sense.

Is it safe to delete a NULL pointer?

delete performs the check anyway, so checking it on your side adds overhead and looks uglier. A very good practice is setting the pointer to NULL after delete (helps avoiding double deletion and other similar memory corruption problems).

I'd also love if delete by default was setting the parameter to NULL like in

#define my_delete(x) {delete x; x = NULL;}

(I know about R and L values, but wouldn't it be nice?)

Is it necessary to null your pointers after deleting multidimensional dynamic array

Memory leak will not happen in your case , but for Good Practice you need to null your pointer after deleted that pointer , otherwise if you later dereference the same variable, your program will have undefined behavior

"unhandled exception error" may occur.

if(arr) 

also not help this kind of case.
so use the null check of every pointer before access the pointer

Why pointer is not NULL after using delete in C++

It's because when you say delete p you're deleting a pointer to memory which completely erases the reference to the new memory you allocated. When you say if p==NULL you're checking to see if the pointer was set to null when in fact the memory that it was pointing to was de-allocated so the pointer isn't pointing to anything. Which doesn't mean the same as having it point to NULL in C++.

Why point a pointer to 0 after*** deleting it?

This is done so that you'll get an immediate error if you ever accidentally try to use it after it's been deleted. Using a pointer that points to deleted memory may sometimes "work", but crash sometime later. By setting it to NULL, you make sure that it's always a bad pointer to use.

Is there any reason to check for a NULL pointer before deleting?

It's perfectly "safe" to delete a null pointer; it effectively amounts to a no-op.

The reason you might want to check for null before you delete is that trying to delete a null pointer could indicate a bug in your program.

Edit

NOTE: if you overload the delete operator, it may no longer be "safe" to delete NULL

Reusing a pointer after `delete`

It depends. Every time you create an object with new, you must delete it after use. In the given function you are deleting to create a new one, but are you also deleting once you are done with the object? It's safer to create your object and have the system delete it when the object is out of scope.

I would avoid if possible as you can create a memory leak if not deleted appropriately.



Related Topics



Leave a reply



Submit