Related Posts

  • No related posts

 

C++ SafeDelete explaind

Posted by Jongerius under Development, General Rant
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars (4 votes, average: 1.50 out of 6)
Loading ... Loading ...

When you browse around the web searching for information on C++ you will often come accros the name SafeDelete, but not everywhere it is explained. So what is the purpose of this homemade function.

Well it’s kinda simple, the function checks if an object has already been deleted. If it hasn’t it will free the memory reserved and nullify the pointer. Nullifying means the value is set to 0, otherwise known as NULL.

So let’s look at what a basic ‘SafeDelete’ function is like:

void SafeDelete(LPVOID *pointer)
{
    if (pointer != NULL)
    {
         delete *pointer;
         pointer = null;
     }
}

Pretty basic function, but it does the trick. There are a dosen variations possible but most of them come down to the same basics.

One Response to “C++ SafeDelete explaind”

aBibi: December 23rd, 2009 at 5:49 pm

void SafeDelete(LPVOID *pointer) {
if (pointer) {
delete pointer;
pointer = NULL;
}
}

the correct way :D

Leave a Reply