Question:
Philosophical question: Why check for the existence of something before deleting it? Why not just delete it?
Interspike
2010-01-28 12:14:11 UTC
If you don't actually care if a record exists in a table for example, and you want to delete that specific record...and you have the unique ID for the record, why would you query to see if the record exists in the table before deleting it? Just delete it!
Four answers:
Jason W-S
2010-01-28 12:28:29 UTC
If by unique id, I see no reason to check first, on existence.



Did someone put an if exists in code you're working on, and you're trying to find out why?



The answer is bad habits.
Mantis
2010-01-28 12:25:07 UTC
I agree. The database is already checking this for you. It's not like if you don't check nobody does. There's a performance hit to the additional checks, and it's pretty safe to assume the database engine can check this faster than you can.



Consider that if you send a query to the database engine to find the record, the database engine has to do all the work to parse the query, load up the appropriate table, and perform the search. Then you tell it do delete, and it has to parse a new query, search for the record from scratch, (this is the second time its searched this index now. Even if it's cached there's a hit there), check to see that it was found, and delete it.



That doesn't mean don't check to see that the delete took place. By all means check to see that your delete worked. If you expect somethign deleted and it wasn't deleted you may have some integrity problems. But if the database system is going to check for the file anyways, let it do the work.



This happens in other programming as well. I see programmers do this all the time:



if (myPointer != NULL)

delete[] myPointer;



The C++ spec already requires the delete operator to check for a NULL before attempting a delete. No need to do it again in your code.



So full agreement from me.





@JetsUnderPats--------------

I stand by what I said. The code you gave absolutely will crash, and the C++ specification does not require delete to check here. But this isn't the case I was talking about. Nor, by the way, is it possible or practical to test this. Your SAFE_MEMORY_DELETION macro isn't any safer. In the same circumstances it would also crash (assuming, as it appears, you are just checking that the pointer is non-zero. It looks like it was truncated by Yahoo Answers.)



If you have this:

int * pointer = 0; //note the = 0 here

delete[] pointer;



This is safe. Feel free to try it. The C++ standard requires that delete check for a null pointer. This is what I was saying is duplicate work. (To be fair it's possible to write your own delete operator. I refer to the standard library new and delete. If you don't set it to NULL then you have undefined behavior.)



This is what so many programmers check for that I objected to:



if (pointer)

delete pointer;



This is a pointless check, which does nothing to protect you from deleting a pointer twice or deleting a pointer that was never allocated. It only prevents you from deleting a null pointer which, ironically, is perfectly safe. And yet I see this code time after time after time. Since the delete has to check for NULL anyways the check is performed twice. I am told (but can't vouch for this) that some compilers optimize the check away.



Incidentally, you wrote that this would work:

char * silly = new char[100];

delete silly;



This is a whole seperate discussion. You should be using delete[] rather than delete.
JetsUnderPats 14-31
2010-01-28 12:27:19 UTC
In some cases, this would cause you problems. Eg, the computer can't always delete something that's not there. This is especially true in pointers and dynamic memory.



for example



int main()

{

char * silly;

delete silly; //crashes

}



In this case silly doesn't point to any data to delete therefore the attempt causes a segmentation fault.



but

char * silly = new char[100];

delete silly;



That works.



#define SAFE_MEMORY_DELETION(pointer){if(pointer)delete pointer; }



now



SAFE_MEMORY_DELETION(silly)



will only delete if needed.





@Mantis -



WRONG. The compiler will not check for you by default. There are within debug release settings some options that will, but that's not the default even in Visual Studio .NET 2008



For example

Install Visual C++ 2008



try



int main()

{

char * test;

delete[] test;

return 0;

}

It will crash. Likewise

char * test;

delete test;



It will also crash, even in debug mode. In release mode all compilers should not place in additional checks. ISO standard also does not provide such checks.





@ mantis -

You're right, i am wrong. While some compilers do optimize the check, I was incorrect in saying they are supposed to. I was under the impression that the optimization was the ISO standard but its not. Likewise, most microsoft sample code always checks, for example if you look at the Direct3D samples they have a SAFE_DELETE macro defined which checks for the pointer, deletes is and nulls it again.



#define SAFE_DELETE(p)

{

if(p)

{ delete p;

p = NULL;

}

}



but you are actually correct in saying the first check is not needed. Coindentally, they have another check for a null pointer on com interface which would be needed because you are accessing a member function to release it.



#define SAFE_RELEASE(p)

{

if(p)

{ p->Release();

p = NULL;

}

}



That would be needed, and of course would require you to intialize the pointer to null for it to work.

But you were right, I was mistaken.
Tubs
2010-01-28 12:54:37 UTC
I don't see any reason not to delete. Just do it!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'Philosophical question: Why check for the existence of something before deleting it? Why not just delete it?' (Questions and Answers)
22
replies
If you could have 1 word erased from the dictionary, along with its definition, what word would it be?
started 2007-04-23 17:31:27 UTC
philosophy
Loading...