What happens if you delete a null pointer




















If you want to learn more in delete vs delete[] please read this article. Post a Comment. November 15, This is a famous coding interview question that every programmer suppose to know.

Here I am going to look at two different answers. Access specifiers friend specifier. Class template Function template. Inline assembly. Compiler support. Freestanding and hosted. Language support library. Technical specifications. Flow control. Function declaration. Lambda function declaration.

Fundamental types. Function types. Compound types. Storage duration specifiers. Default initialization. Value initialization. Zero initialization. Copy initialization. Direct initialization. Some people like to do this in the spirit of defensive programming: it will make program behaviour slightly more predictable in the case of a bug: accessing the pointer after deletion will result in a null pointer access rather than a access to a random memory location.

Although both operations are undefined behaviour, the behaviour of a null pointer access is a lot more predictable in practice it most often results in a direct crash rather than memory corruption.

Since memory corruptions are especially hard to debug, resetting deleted pointers aids debugging. You should treat resetting pointers as code smell. This function gets called internally for every call of operator delete with matching arguments. The same is true for operator new.

Overloading operator new and then also operator delete makes sense in some situations when you want to control precisely how memory is allocated. Doing this isn't even very hard, but a few precautions must be made to ensure correct behaviour. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.

A very good practice is setting the pointer to NULL after delete helps avoiding double deletion and other similar memory corruption problems. Of course, no one would ever do 'delete' of a pointer with NULL value, but it is safe to do. Ideally one should not have code that does deletion of a NULL pointer. But it is sometimes useful when deletion of pointers e. The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.

If ptr is a null pointer, no action occurs. There's no harm in deleting a null pointer; it often reduces the number of tests at the tail of a function if the unallocated pointers are initialized to zero and then simply deleted.

Since the previous sentence has caused confusion, an example — which isn't exception safe — of what is being described:. There are all sorts of nits that can be picked with the sample code, but the concept is I hope clear.

The pointer variables are initialized to zero so that the delete operations at the end of the function do not need to test whether they're non-null in the source code; the library code performs that check anyway. Deleting a null pointer has no effect. It's not good coding style necessarily because it's not needed, but it's not bad either.

If you are searching for good coding practices consider using smart pointers instead so then you don't need to delete at all. It is safe unless you overloaded the delete operator. There is a FAQ on this matter which answers this question. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Is it safe to delete a NULL pointer?



0コメント

  • 1000 / 1000