C++ SafeDelete explained

By | August 17, 2007

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 thought on “C++ SafeDelete explained

  1. aBibi

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

    the correct way 😀

    Reply

Leave a Reply